Search Unity

Object follows touch.position

Discussion in 'Scripting' started by loobys, Apr 28, 2018.

  1. loobys

    loobys

    Joined:
    Apr 25, 2018
    Posts:
    3
    The point of our script is for the ball to follow our finger when placed on the screen but since its following where ever we place our finger it will teleport to any new location instead of just smoothly going to where are finger is. The code does work, however it follows our finger but every time i release my finger and put it back down it will teleport to that new position. I don't know how to fix it, I'm a beginner.
    This is for a 2D game
    This is my code:
    Code (CSharp):
    1.     void Update () {
    2.         if (Input.touchCount > 0) {
    3.             // The screen has been touched so store the touch
    4.             Touch touch = Input.GetTouch (0);
    5.  
    6.             if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) {
    7.                 Vector3 touchPosition = Camera.main.ScreenToWorldPoint (new Vector3 (touch.position.x, touch.position.y, 10));            
    8.                 transform.position = touchPosition;
    9.             }
    10.  
    11.             }
    12.         }
     
    Last edited: Apr 28, 2018
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    shekalo likes this.