- Home /
 
               Question by 
               Pepa01 · Jun 26, 2014 at 06:12 PM · 
                2drotationtouchrotate object  
              
 
              2D - Rotate object based on touch moved (diff between touches)
Hi,
I´m newbie in Unity. I want rotate my 2D object based on user touch moved (moved finger on the screen). I have this code:
 void Update ()
 {
         if (Input.touches.Length > 0) {
              t = Input.GetTouch (0);
     
             if (t.phase == TouchPhase.Moved) {
                 Vector3 movePos = new Vector3 (t.position.x, t.position.y, 0);
                 var objectPos = Camera.main.WorldToScreenPoint (transform.position);
                 var dir = movePos - objectPos; 
                 transform.rotation = Quaternion.Euler (new Vector3 (0f, 0f, Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg));
             }
         }
 }
This code rotate the object based on user touch but when I touch screen again in another position and do touch move, it will rotate the whole object to the actual touch and then it will do correct object rotation based on touch move.
And I dont´t want rotate the whole object based on touch position but rotate the object only based on touch move. Do you understand me? Can you help me? How should I rewrite my code?
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                