- Home /
Smooth rotation when dragging on mobile(android)
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.
 #pragma strict
 
 private var oldMousePos : float;
 private var curMousePos : float;
 var old : float;
 var touch1 : float;
 var dual : boolean;
 
 function Update () 
 {    
     if (Input.touchCount == 1)
     {
         touch1 =Input.GetTouch(0).position.x;
     
         if (Input.GetTouch(0).phase == TouchPhase.Began  )
         {
             oldMousePos = (touch1/Screen.width);
             old = 0;
         }
         if(Input.GetTouch(0).phase == TouchPhase.Moved )// This keeps running while finger is held down
         {
             curMousePos =  (touch1/Screen.width) - oldMousePos;
             
             if(!dual){
             transform.Rotate(0,((curMousePos-old)*20000)*Time.deltaTime ,0);
             }
             
             else{dual = false;}
             old = curMousePos;
         }
     }
     if(Input.touchCount == 2 )
     {
         dual = true;
     }
 }
Also posted here: http://forum.unity3d.com/threads/136415-Smooth-rotation-when-dragging?p=926264#post926264
thanks for the code! appreciated. can i ask how can i drag an object with touch?
Answer by recon472 · Nov 02, 2014 at 02:42 PM
Have you tried sth like
 float smooth = some_number; // bigger the number the faster it will rotate
 Quaternion newRot = transform.Rotate(0,curMousePos-old,0);
 transform.rotation = Quaternion.Lerp(transform.rotation,newRot,Time.deltaTime * smooth);
Not tested but it should work. this is c# though but It's easy to read
Answer by Prasanna · Dec 12, 2014 at 06:28 AM
@jeff25, just simply drag this script into your gameobject. Make sure your gameobject tag is Draggable or you could change that.
 using UnityEngine;
 using System.Collections;
 
 public class Drag : MonoBehaviour 
 {
     // Touch distance to the object.
     private float dist;
     // Checking condition for touching the object.
     private bool dragging = false;
     // Distance of the object and the touch.
     private Vector3 offset;
     // Drag the object.
     private Transform toDrag;
     
     void Update() 
     {
         Vector3 v3;
         
         if (Input.touchCount != 1) 
         {
             dragging = false; 
             return;
         }
         
         Touch touch = Input.touches[0];
         Vector3 pos = touch.position;
 
         // Started the touch.
         if(touch.phase == TouchPhase.Began) 
         {
             // Finger starts the touch.
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(pos); 
             // Finger on object.
             if(Physics.Raycast(ray, out hit) && (hit.collider.tag == "Draggable"))
             {
                 Debug.Log ("Here");
                 // Object to finger.
                 toDrag = hit.transform;
                 dist = hit.transform.position.z - Camera.main.transform.position.z;
                 v3 = new Vector3(pos.x, pos.y, dist);
                 v3 = Camera.main.ScreenToWorldPoint(v3);
                 offset = toDrag.position - v3;
                 dragging = true;
             }
         }
         // Check finger drag the object to the display.
         if (dragging && touch.phase == TouchPhase.Moved) 
         {
             v3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
             v3 = Camera.main.ScreenToWorldPoint(v3);
             // Drag object at particular distance.
             toDrag.position = v3 + offset;
         }
         // Finger release the Object/Screen.
         if (dragging && (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)) 
         {
             dragging = false;
         }
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
Rotating a character smoothly 0 Answers
Add tilt function to rotate a ball android device 0 Answers
Rotate camera with easeout 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                