Search Unity

Resolved How do I make touch to behave similarly to mouse in my ClickToDrag acript?

Discussion in 'Scripting' started by farazk86, Jun 14, 2020.

  1. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    195
    Hi,

    I have a script that controls the rotation of a plane. Click to drag and the drag decides the rotation. Keeping it level is the target of this script so as the ball does not fall off. I have written below script for this to work:

    Code (CSharp):
    1.  public float rotSpeed = 20;
    2.     float zDeg;
    3.     float xDeg;
    4.     Quaternion fromRotation;
    5.     Quaternion toRotation;
    6.  
    7.     void Start()
    8.     {
    9.        
    10.     }
    11.  
    12.     void Update()
    13.     {
    14.         if (Input.GetMouseButton(0))
    15.         {
    16.             zDeg += Input.GetAxis("Mouse X") * rotSpeed;        // I had to add Mouse X to the input manager for this to work, use mouse movement only and remove gravity etc and other settings
    17.             xDeg -= Input.GetAxis("Mouse Y") * rotSpeed;
    18.  
    19.             zDeg = Mathf.Clamp(zDeg, -40f, 40f);
    20.             xDeg = Mathf.Clamp(xDeg, -40f, 40f);
    21.  
    22.             fromRotation = transform.rotation;
    23.             toRotation = Quaternion.Euler(xDeg, 0, zDeg);       // no wwanting to rotate the table around y-axis, so setting 0
    24.             transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * 5.0f);
    25.         }
    26.  
    27.     }
    It works great on mouse and is easy to control as well. But when I built for mobile the behavior is very different.

    At times, when I touch the screen the plane will jump to an extreme starting position causing the ball on it to fall off. Looks like depending on where I touch the plane will take that as its starting position. Can I ensure that does not happen?

    Thanks
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Do you use this for mobile, or do you actually replace mouse inputs with touch inputs? I would recommend actually using the provided Touch interface instead of GetAxis(Mouse). Then you have access to all finger coordinates and can determine a delta between the current and last position to get your zDeg and xDeg.
     
    farazk86 likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
  4. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    195
    Thank you for letting me know about mouse not same as touch

    I simply re-wrote my above script with touch to make it work as expected on mobile

    Code (CSharp):
    1. if (Input.touchCount == 1)
    2.         {
    3.             Touch touch = Input.GetTouch(0);
    4.             if (touch.phase == TouchPhase.Moved)
    5.             {
    6.                 zDeg += touch.deltaPosition.x * rotSpeed;
    7.                 xDeg -= touch.deltaPosition.y * rotSpeed;
    8.  
    9.                 zDeg = Mathf.Clamp(zDeg, -40f, 40f);
    10.                 xDeg = Mathf.Clamp(xDeg, -40f, 40f);
    11.  
    12.                 fromRotation = transform.rotation;
    13.                 toRotation = Quaternion.Euler(xDeg, 0, zDeg);       // no wwanting to rotate the table around y-axis, so setting 0
    14.                 transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * 5.0f);
    15.             }
    16.         }