Search Unity

Rotate a cube using touch inputs

Discussion in 'Scripting' started by Gonzasmpl, Jul 23, 2019.

  1. Gonzasmpl

    Gonzasmpl

    Joined:
    Jul 30, 2018
    Posts:
    55
    Hi, I'm trying to discover how to use touch inputs properly. I have read some pages on the unity manual and some forums as well but then, when I go to unity, things works unexpectedly.

    I want to be able to rotate a cube (on Y axis) sliding my fingers on the screen of a mobile device so I made use of touch phases, comparing touch positions and that, but I have some undesirable behaviours like "Insta Rotating" the moment a finger touches the screen and also some shakes on the cube rotation at the end of the movement when the finger is not moving anymore but it is still touching the screen.

    I have a line of code that rotates the cube on Y axis using the mouse which works perfectly for me:
    Code (CSharp):
    1. private void Update()
    2.     {
    3.         if (Input.GetMouseButton(0))
    4.         {
    5.             transform.Rotate(0, Input.GetAxis("Mouse X") * camRotationSensitivity * Time.deltaTime, 0);
    6.         }
    7.      }
    I pretty much want the same behaviour but using touchs inputs.
    If you have some advice to give me in this matter I would be grateful.
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,739
  3. Gonzasmpl

    Gonzasmpl

    Joined:
    Jul 30, 2018
    Posts:
    55
    Thanks! It works.
     
unityunity