Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Smooth rotation when dragging

Discussion in 'Android' started by jukke, May 18, 2012.

  1. jukke

    jukke

    Joined:
    Jan 29, 2010
    Posts:
    80
    Alright so this script basically rotates an object when dragging across the screen with one finger. I would like to make it more "smooth" thou. Ive been trying alot of diffrent things like lerp and slerp and so on but nothing seems to work, so I thought I might as well post the working code here before I give up. I would greatly appricate if anyone knew to to make the rotation more smooth.

    Code (csharp):
    1. #pragma strict
    2.  
    3. private var oldMousePos : float;
    4. private var curMousePos : float;
    5. var old : float;
    6. var touch1 : float;
    7. var dual : boolean;
    8.  
    9. function Update ()
    10. {    
    11.     if (Input.touchCount == 1)
    12.     {
    13.         touch1 =Input.GetTouch(0).position.x;
    14.    
    15.         if (Input.GetTouch(0).phase == TouchPhase.Began  )
    16.         {
    17.             oldMousePos = (touch1/Screen.width);
    18.             old = 0;
    19.         }
    20.         if(Input.GetTouch(0).phase == TouchPhase.Moved )// This keeps running while finger is held down
    21.         {
    22.             curMousePos =  (touch1/Screen.width) - oldMousePos;
    23.            
    24.             if(!dual){
    25.             transform.Rotate(0,((curMousePos-old)*200) ,0);
    26.             }
    27.            
    28.             else{dual = false;}
    29.             old = curMousePos;
    30.         }
    31.     }
    32.     if(Input.touchCount == 2 )
    33.     {
    34.         dual = true;
    35.     }
    36. }
     
unityunity