Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Touch Event is called even outside the attached script

Discussion in 'Scripting' started by biggdawgg, Jan 27, 2021.

  1. biggdawgg

    biggdawgg

    Joined:
    Jan 2, 2015
    Posts:
    4
    So I am totally new to Unity and started development. My 2D Hierarchy looks like this.

    -> Scene
    -> Main Camera
    -> The background
    -> The Player

    I have script attached only to PLAYER . The script is next:
    Code (CSharp):
    1.  
    2. // The file 'PlayerController.cs' was created by Vladimir
    3. // Creation Date - 27.01.2021 4:16:34
    4. // All rights are reserverd
    5.  
    6. using UnityEngine;
    7.  
    8. /// <summary>
    9. /// Defines the <see cref="PlayerController" />.
    10. /// </summary>
    11. public class PlayerController : MonoBehaviour
    12. {
    13.     // Start is called before the first frame update
    14.     private Vector2 pos;
    15.     internal void Start()
    16.     {
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     internal void Update()
    21.     {
    22.  
    23.         if (Input.touchCount > 0)
    24.         {
    25.             pos = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
    26.             if (Input.GetTouch(0).phase == TouchPhase.Moved)
    27.             {
    28.                 // change '2' to a variable that you can tune in the inspector, to get a speed you like.
    29.                 transform.position = Vector3.MoveTowards(transform.position, pos, 2 * Time.deltaTime);
    30.  
    31.             }
    32.  
    33.         }
    34.     }
    35.  
    36. }
    So everything works fine (Nasty non-smooth moving though) but the issue is that if I touch anywhere outside my PLAYER will still follow my touched place... I thought script is only working with the object you have attached to it? Isn't it? Only my player has a 2D Box Collider. The event so fired up even if my object wasn't touched. Any clue why this happening or is this an expected behavior?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Input.GetTouch(0) is just the first finger touching the screen.

    It doesn't care about anything else.

    Look into methods such as OnMouseDown() or else implement your own raycast into the scene to see if you hit the player or not.

    Personally I would suggest doing your own cast-to-hit or distance check because I'm not sure what OnMouseDown() even does when there is no mouse, or with multiple finger touches.
     
    biggdawgg likes this.
  3. biggdawgg

    biggdawgg

    Joined:
    Jan 2, 2015
    Posts:
    4
    Thanks a lot! Well speaking about being able to see if player was hit or no, wouldn't OnCollisionEnter of Collider component be better solution than raycast implementation?
     
  4. biggdawgg

    biggdawgg

    Joined:
    Jan 2, 2015
    Posts:
    4
    OnMouseDown doesn't give any touch X.Y unfortunately so I can't get a hold of it. I need the character to follow my finger and touch event seems to be the only solution?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Not really. That's for colliding two colliders under control of the physics engine. A Raycast is an imaginary line that can hit colliders and tell you about what it hit (via the returned RaycastHit object).
     
    biggdawgg likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Not at all. You probably want to try out a few of the many Youtube tutorials on touch control.
     
    biggdawgg likes this.