• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by jukke · May 17, 2012 at 11:48 PM · androidrotatedragsmooth

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jeff25 · Jun 23, 2014 at 02:10 AM 0
Share

thanks for the code! appreciated. can i ask how can i drag an object with touch?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
         }
     }
 }



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Return to the origin of rotation 0 Answers

Turn page, smooth rotate 1 Answer

OnTriggerEnter smooth Rotate 1 Answer

smooth rotate help 1 Answer

90 Degree Smooth Rotate On Button Press 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges