Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Why touch.deltaposition takes reference of previous touch? (Legacy Input)

Discussion in 'Input System' started by Solidcomer, Jul 16, 2021.

  1. Solidcomer

    Solidcomer

    Joined:
    Sep 12, 2017
    Posts:
    95
    I'm using Legacy Input, touch.deltaPositionto control virtual camera rotation.

    In one script, camera only rotates when drag, but in another script, camera rotates right after I touch the screen and just begin the drag.

    My code is very simple, the script 1 gives desired result, that the camera rotates only when I drag. During the test, all touches are 1 finger touch and drag.

    I hope to know why in Script 2 with the virtual camera 3rd person follow mode, the camera rotates as soon as I touched the screen before I begin to drag.

    Script 1, desired - CinemachineFreeLook
    Code (CSharp):
    1.     private CinemachineFreeLook currentVCam;
    2.     private float dragSpeed = 1f;
    3.  
    4.     public void Update()
    5.     {
    6.         if (Input.touchCount > 0)
    7.         {
    8.             foreach (var touch in Input.touches)
    9.             {
    10.                     currentVCam.m_XAxis.m_InputAxisValue = touch.deltaPosition.x * dragSpeed * Time.deltaTime;
    11.                     currentVCam.m_YAxis.m_InputAxisValue = touch.deltaPosition.y * dragSpeed * Time.deltaTime;
    12.                     break;
    13.             }
    14.         }
    15.    }

    Script 2, not desired - Virtual Camera, 3rd Person Follow
    Code (CSharp):
    1. public Cinemachine.AxisState yAxis;
    2.     public Cinemachine.AxisState xAxis;
    3.     private float dragSpeed = 1f;
    4.     public Transform LookAtTarget;
    5.  
    6.     void Update()
    7.     {
    8.         if (Input.touchCount > 0)
    9.         {
    10.             foreach (var touch in Input.touches)
    11.             {
    12.                     xAxis.m_InputAxisValue = touch.deltaPosition.x * dragSpeed * Time.deltaTime;
    13.                     yAxis.m_InputAxisValue = touch.deltaPosition.y * dragSpeed * Time.deltaTime;
    14.                     break;
    15.             }
    16.         }
    17.  
    18.         xAxis.Update(Time.deltaTime);
    19.         yAxis.Update(Time.deltaTime);
    20.         LookAtTarget.eulerAngles = new Vector3(yAxis.Value, xAxis.Value, 0f);
    21.     }
    Thanks very much.
     
    Last edited: Jul 16, 2021
  2. Solidcomer

    Solidcomer

    Joined:
    Sep 12, 2017
    Posts:
    95
    I just leave Input Axis Name to blank, and everything is fine.