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

Touch dont move

Discussion in 'Editor & General Support' started by megalphax, Aug 25, 2020.

  1. megalphax

    megalphax

    Joined:
    Jan 10, 2020
    Posts:
    9
    Hello, someone know why the touch.position dont move when i touch the screen ?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Ball : MonoBehaviour
    6. {
    7.     Touch touch;
    8.     Vector2 touchPos;
    9.     float deltaX;
    10.     Vector3 newXPos;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.        
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if(Input.touchCount > 0)
    22.         {
    23.             touch = Input.GetTouch(0);
    24.             touchPos = Camera.main.ScreenToWorldPoint(touch.position);
    25.  
    26.             switch (touch.phase)
    27.             {
    28.                 case TouchPhase.Began:
    29.                     deltaX = touchPos.x - transform.position.x;
    30.                     Debug.Log(touchPos);
    31.                     break;
    32.  
    33.                 case TouchPhase.Moved:
    34.                     newXPos = new Vector3(touchPos.x - deltaX, 0, 0);
    35.                     transform.position = Vector3.MoveTowards(transform.position, newXPos, Time.deltaTime);
    36.                     break;
    37.  
    38.                 case TouchPhase.Ended:
    39.                    
    40.                     break;
    41.                
    42.             }
    43.         }
    44.     }
    45. }
    46.  
     
  2. megalphax

    megalphax

    Joined:
    Jan 10, 2020
    Posts:
    9
    please it's really important
     
  3. megalphax

    megalphax

    Joined:
    Jan 10, 2020
    Posts:
    9