Unity - How to rotate an object based on touch?

19,491

Your code is a little weird. Meaning you use the x and y values in the wrong spots of the Vecotr. I'm going to assume you did this on purpose to get the cube to turn correctly. I would use the Rotate instead of changing the rotation value.

Here is a better than I can do: http://answers.unity3d.com/questions/255118/rotating-an-object-with-touch.html

Share:
19,491
Ashish Beuwria
Author by

Ashish Beuwria

Updated on June 04, 2022

Comments

  • Ashish Beuwria
    Ashish Beuwria almost 2 years

    I'm creating a game for iOS devices. I want my player or gameobject to rotate based on touch. I'm using the following code in touchPhase.Moved

    if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) 
        {
            coinTouched = true;
    
            if (coinTouched) 
            {
                Vector2 currentTouchPosition = Input.GetTouch (0).position;
                endPos = new Vector3 (currentTouchPosition.x, 0, currentTouchPosition.y);
                endTime = Time.time;
    
    
                gameObject.transform.rotation = Quaternion.Euler(0f,Input.GetTouch(0).deltaPosition.x * 1.0f,0f);
    
            }
    
        }
    

    But it is behaving awkward. it rotates good for almost 180 degree and then it moves in the opposite direction and after another 180 it rotates in the opposite direction and so on. Please help me.

    Thank You.