Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Input.touch (0) system needs rework please take a look in my script

Discussion in 'Scripting' started by chrisrcisme, Oct 16, 2018.

  1. chrisrcisme

    chrisrcisme

    Joined:
    Jul 7, 2014
    Posts:
    16
    Hello all :)
    Im in need of some help to improve my player control system if you could take the time to read and give some me some improvements or advice i would really appreciate it :)

    and sorry if it looks a little messy with the // comments in the script or anywhere else im still new as i knew nothing about coding before june this year (2018)

    What i want to do is make it so while clickhelddown is true and touching the screen with one finger to be able to walk with a second finger.
    A second thing is that when a second finger is pressed while the first finger is held down to aim the second finger being pressed makes the aim function calculate some average angle between each touch instead of aiming at the first finger touch and im not sure how to fix this.

    Many thanks if you have read past this :) :)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.AI;
    3. using UnityEngine.UI;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class ClickToMove : MonoBehaviour
    7. {
    8.     NavMeshAgent Agent;
    9.     Rigidbody playerRigidbody;
    10.     SpriteRenderer footprintsprite, footprintcopysprite;
    11.     GameObject footprintcopy;
    12.     float clickStart;
    13.     bool playerIsAiming = false;
    14.  
    15.     public LayerMask focusLayers;
    16.     public GameObject guntip, footprint;
    17.     public float clickStopWatch = 0;  
    18.     public float clickToWalk = 0.3f;
    19.     public bool clickHeldDown = false;
    20.     public static bool updateRotation;
    21.     public float remainingDistance;
    22.  
    23.         /*Ray GenerateMouseRay ( Vector3 touchPos )
    24.         {
    25.                 Vector3 mousePosFar = new Vector3 ( touchPos.x , touchPos.y , Camera.main.farClipPlane );
    26.  
    27.                 Vector3 mousePosNear = new Vector3 ( touchPos.x , touchPos.y , Camera.main.nearClipPlane );
    28.         Vector3 mousePosF = Camera.main.ScreenToWorldPoint ( mousePosFar );
    29.         Vector3 mousePosN = Camera.main.ScreenToWorldPoint ( mousePosNear );
    30.  
    31.         Ray mr = new Ray ( mousePosN , mousePosF - mousePosN );
    32.         return mr;
    33.  
    34.         }*/
    35.  
    36.     void Start()
    37.         {
    38.         Agent = GetComponent<NavMeshAgent>();
    39.         playerRigidbody = GetComponent <Rigidbody> ();
    40.         Agent.updateRotation = false;
    41.         footprintsprite = footprint.GetComponent<SpriteRenderer>();
    42.         footprintsprite.enabled = false;
    43.         }
    44.  
    45.     void Update()
    46.     {  
    47.         // If click ends with less than 0.3 seconds and not ended on ui go to walk function
    48.         if ( clickStopWatch < clickToWalk & Input.GetTouch (0).phase == TouchPhase.Ended  )
    49.             {
    50.             Walk ();
    51.             }
    52.  
    53.         // If click ended atall click is false
    54.         if ( Input.GetTouch (0).phase == TouchPhase.Ended )
    55.             {
    56.             //click has been released
    57.             clickHeldDown = false;
    58.             playerIsAiming = false;
    59.             }
    60.        
    61.         if (Input.touchCount > 0 )
    62.         {
    63.             clickStopWatch += Time.deltaTime;
    64.            
    65.             clickToAimCheck ();
    66.                                     // If touch phase began and current pointer is not over a UI game object
    67.                 if (Input.GetTouch (0).phase == TouchPhase.Began & !EventSystem.current.IsPointerOverGameObject (Input.GetTouch(0).fingerId )  )
    68.                     {
    69.                     Debug.Log ("touchphase.began has happened");
    70.                     clickHeldDown = true;
    71.                
    72.                     }
    73.         }
    74.         remainingDistance = Agent.remainingDistance;
    75.        
    76.         if (clickHeldDown == false)
    77.         {
    78.         clickStopWatch = 0;
    79.         }
    80.         /*////Code for computer input
    81.         if ( Input.GetMouseButtonUp (0) & clickStopWatch <= clickToWalk )
    82.             {
    83.                 if ( EventSystem.current.IsPointerOverGameObject () )
    84.                     {
    85.                         return;
    86.                     }
    87.                 else
    88.                 Walk ();
    89.                 //Debug.Log ("Mouse button has been released and clickstopwatch was less than 0.3 seconds");
    90.             }
    91.        
    92.         if (Input.GetMouseButtonUp(0))
    93.         {
    94.         clickHeldDown = false;
    95.         }
    96.            
    97.         if (Input.GetMouseButton (0) )
    98.         {  
    99.         clickHeldDown = true;
    100.         clickStopWatch += Time.deltaTime;
    101.         clickToWalkCheck ();
    102.         }*/
    103.        
    104.         if (remainingDistance <= 0.1f & clickHeldDown == false & Agent.hasPath == true )
    105.         {
    106.             Agent.ResetPath();
    107.         }
    108.     }
    109.  
    110.     void Turning ()
    111.     {
    112.         playerIsAiming = true;
    113.  
    114.         // Create a ray from the mouse cursor on screen in the direction of the camera.
    115.         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    116.  
    117.         // Create a RaycastHit variable to store information about what was hit by the ray.
    118.         RaycastHit floorHit;
    119.  
    120.         // Perform the raycast and if it hits something on the floor layer...
    121.         if(Physics.Raycast (camRay, out floorHit, 120f , focusLayers))
    122.         {
    123.             // Create a vector from the player to the point on the floor the raycast from the mouse hit.
    124.             Vector3 playerToMouse = floorHit.point - transform.position;
    125.  
    126.             // Ensure the vector is entirely along the floor plane.
    127.             playerToMouse.y = 0f;
    128.  
    129.             // Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
    130.             Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
    131.  
    132.             // Set the player's rotation to this new rotation.
    133.             playerRigidbody.MoveRotation (newRotation);
    134.             //Debug.Log ("im using the turning function");
    135.         }
    136.     }
    137.  
    138. void clickToAimCheck()
    139.     {
    140.         //If stop watch is greater than 0.3 seconds..
    141.     if ( clickStopWatch > clickToWalk & playerIsAiming == false )
    142.             {
    143.                Turning ();
    144.  
    145.             }
    146.     else  
    147.             {//If stop watch is less than 0.3 seconds
    148.             return;
    149.             }
    150.     }
    151.     void Walk()
    152.         {   // This stops false walking
    153.             if (clickHeldDown == false)
    154.                 {
    155.                 return;
    156.                 }
    157.                     RaycastHit hit;
    158.                     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //from original script
    159.                     Debug.DrawRay(ray.origin, ray.direction * 120 , Color.black, 5.0f);
    160.                     //Debug.Log ("Im shooting my raycast to walk");
    161.                     if (Physics.Raycast ( ray , out hit , 120f , focusLayers , QueryTriggerInteraction.Ignore )) //Ignore means to ignore trigger gameobjects
    162.                        
    163.                         if (hit.collider.tag != "enemy" )
    164.                         {
    165.                         Agent.SetDestination ( hit.point );
    166.                         footprintcopy = Instantiate ( footprint , Agent.destination , Quaternion.identity  );
    167.                         footprintcopysprite = footprintcopy.GetComponent <SpriteRenderer>();
    168.                         footprintcopysprite.enabled = true;
    169.                         footprintcopy.transform.eulerAngles = new Vector3 ( 90, 0 , 0 );
    170.                         clickHeldDown = false;
    171.                         }
    172.                 }
    173. }
    174.  
     
  2. chrisrcisme

    chrisrcisme

    Joined:
    Jul 7, 2014
    Posts:
    16
    I've managed to fix this myself after a little change in the walk function for screenpointtoray ( .position )
    if anyone is interested here is the re worked version :) im open to suggestions and any critics out there

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.AI;
    3. using UnityEngine.UI;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class ClickToMove : MonoBehaviour
    7. {
    8.     NavMeshAgent Agent;
    9.     Rigidbody playerRigidbody;
    10.     SpriteRenderer footprintsprite, footprintcopysprite;
    11.     GameObject footprintcopy;
    12.     float clickStart;
    13.     Touch myFirstTouch;
    14.  
    15.     public LayerMask focusLayers;
    16.     public GameObject guntip, footprint;
    17.     public float clickStopWatch = 0;  
    18.     public float clickToWalk = 0.3f;
    19.     public bool clickHeldDown = false;
    20.     public static bool updateRotation;
    21.     public float remainingDistance;
    22.  
    23.         /*Ray GenerateMouseRay ( Vector3 touchPos )
    24.         {
    25.                 Vector3 mousePosFar = new Vector3 ( touchPos.x , touchPos.y , Camera.main.farClipPlane );
    26.  
    27.                 Vector3 mousePosNear = new Vector3 ( touchPos.x , touchPos.y , Camera.main.nearClipPlane );
    28.         Vector3 mousePosF = Camera.main.ScreenToWorldPoint ( mousePosFar );
    29.         Vector3 mousePosN = Camera.main.ScreenToWorldPoint ( mousePosNear );
    30.  
    31.         Ray mr = new Ray ( mousePosN , mousePosF - mousePosN );
    32.         return mr;
    33.  
    34.         }*/
    35.  
    36.     void Start()
    37.         {
    38.         Agent = GetComponent<NavMeshAgent>();
    39.         playerRigidbody = GetComponent <Rigidbody> ();
    40.         Agent.updateRotation = false;
    41.         footprintsprite = footprint.GetComponent<SpriteRenderer>();
    42.         footprintsprite.enabled = false;
    43.         }
    44.  
    45.     void Update()
    46.     {  
    47.         /* //Needs some little work done to fix walking to be acurate
    48.         #if UNITY_EDITOR || UNITY_STANDALONE
    49.        
    50.         if ( Input.GetMouseButtonUp (0) & clickStopWatch <= clickToWalk )
    51.             {
    52.                 if ( EventSystem.current.IsPointerOverGameObject () )
    53.                     {
    54.                         return;
    55.                     }
    56.                 else
    57.                 Walk ();
    58.                 //Debug.Log ("Mouse button has been released and clickstopwatch was less than 0.3 seconds");
    59.             }
    60.        
    61.         if (Input.GetMouseButtonUp(0))
    62.         {
    63.         clickHeldDown = false;
    64.         }
    65.            
    66.         if (Input.GetMouseButton (0) )
    67.         {  
    68.         clickHeldDown = true;
    69.         clickStopWatch += Time.deltaTime;
    70.         clickToAimCheck ();
    71.         }
    72.  
    73.         #endif */
    74.  
    75.  
    76.     #if UNITY_IOS || UNITY_ANDROID
    77.  
    78.         // If click ends with less than 0.3 seconds and not ended on ui go to walk function
    79.         if ( clickStopWatch < clickToWalk & myFirstTouch.phase == TouchPhase.Ended  )
    80.             {
    81.             Walk ();
    82.             }
    83.  
    84.         // If click ended atall click is false
    85.         if ( myFirstTouch.phase == TouchPhase.Ended )
    86.             {
    87.             //click has been released
    88.             clickHeldDown = false;
    89.             }
    90.        
    91.         if (Input.touchCount > 0 )
    92.         {
    93.             myFirstTouch = Input.touches [0];
    94.             clickStopWatch += Time.deltaTime;
    95.             clickToAimCheck ();
    96.                                    
    97.                                     // If first touch phase began and current pointer is not over a UI game object
    98.                 if (myFirstTouch.phase == TouchPhase.Began & !EventSystem.current.IsPointerOverGameObject ( Input.touches [0].fingerId )  )
    99.                     {
    100.                     Debug.Log ("touchphase.began has happened");
    101.                     clickHeldDown = true;
    102.                     }
    103.         }
    104.  
    105.         remainingDistance = Agent.remainingDistance;
    106.        
    107.         if (clickHeldDown == false)
    108.         {
    109.         clickStopWatch = 0;
    110.         }
    111.        
    112.         if (remainingDistance <= 0.1f & clickHeldDown == false & Agent.hasPath == true )
    113.         {
    114.             Agent.ResetPath();
    115.         }
    116.         #endif
    117.     }
    118.  
    119.     void Turning ()
    120.     {
    121.         // Create a ray from the mouse cursor on screen in the direction of the camera.
    122.         Ray camRay = Camera.main.ScreenPointToRay (myFirstTouch.position);
    123.  
    124.         // Create a RaycastHit variable to store information about what was hit by the ray.
    125.         RaycastHit floorHit;
    126.  
    127.         // Perform the raycast and if it hits something on the floor layer...
    128.         if(Physics.Raycast (camRay, out floorHit, 120f , focusLayers))
    129.         {
    130.             // Create a vector from the player to the point on the floor the raycast from the mouse hit.
    131.             Vector3 playerToMouse = floorHit.point - transform.position;
    132.  
    133.             // Ensure the vector is entirely along the floor plane.
    134.             playerToMouse.y = 0f;
    135.  
    136.             // Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
    137.             Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
    138.  
    139.             // Set the player's rotation to this new rotation.
    140.             playerRigidbody.MoveRotation (newRotation);
    141.             //Debug.Log ("im using the turning function");
    142.         }
    143.     }
    144.  
    145. void clickToAimCheck()
    146.     {
    147.         //If stop watch is greater than 0.3 seconds..
    148.     if ( clickStopWatch > clickToWalk )// testing...& playerIsAiming == false )
    149.             {
    150.                Turning ();
    151.             }
    152.     else  
    153.             {//If stop watch is less than 0.3 seconds
    154.             return;
    155.             }
    156.     }
    157.     void Walk()
    158.         {   // This stops walking while touching UI because !EventSystem.current.IsPointerOverGameObject ( Input.touches [0].fingerId )
    159.             // Only works with touchphase.began...clickheld down is true while touchphase.began (never over UI) happens in one frame
    160.             //then is false when touchphase.ended
    161.             if (clickHeldDown == false)
    162.                 {
    163.                 return;
    164.                 }
    165.                     RaycastHit hit;
    166.                     Ray ray = Camera.main.ScreenPointToRay(myFirstTouch.position); //from original script
    167.                     Debug.DrawRay(ray.origin, ray.direction * 120 , Color.black, 5.0f);
    168.                     //Debug.Log ("Im shooting my raycast to walk");
    169.                     if (Physics.Raycast ( ray , out hit , 120f , focusLayers , QueryTriggerInteraction.Ignore )) //Ignore means to ignore trigger gameobjects
    170.                        
    171.                         if (hit.collider.tag != "enemy" )
    172.                         {
    173.                         Agent.SetDestination ( hit.point );
    174.                         footprintcopy = Instantiate ( footprint , Agent.destination , Quaternion.identity  );
    175.                         footprintcopysprite = footprintcopy.GetComponent <SpriteRenderer>();
    176.                         footprintcopysprite.enabled = true;
    177.                         footprintcopy.transform.eulerAngles = new Vector3 ( 90, 0 , 0 );
    178.                         clickHeldDown = false;
    179.                         }
    180.                 }
    181. }
    182.  
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520