• 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
0
Question by Paul Graystone · Jan 23, 2013 at 11:22 PM · rotationjavascriptmousequaternion

Rotate object following mouse movement. Object jumps/ flips

I have a little gnome sitting on a round platform. With a mouse click on the platform I want to rotate the platform around it's Y axis. The angle depends on the position of the mouse.

My implementation is based on the idea of t$$anonymous$$s thread about how to rotate a door according to mouse movement.

Down below is my full script, w$$anonymous$$ch is attached to the platform.

The problem: whenever I click on a random point on the platform, the platform will jump/ flip to an unpredictable rotation angle. I tried to prevent that with the rotationStart = transform.rotation. Unfortunately t$$anonymous$$s won't prevent the platform from flipping. What can I do to ac$$anonymous$$eve a smooth rotation?

EDIT: T$$anonymous$$s is the kind of rotation I am trying to ac$$anonymous$$eve:

The left image displays the rotation behavior when applying robertbu's solution. The red cross symbolizes the mouse click position.

I want the rotation behavior shown on the right image.

alt text

Current Code

 private var mainCam;
 private var firstHitPos;
 private var rotationPlane : Plane;
 var speed = 0.1;
 
 function Start () {
     mainCam         = GameObject.FindWithTag("MainCamera");
 }
 
 function OnMouseDown(){
         var ray = mainCam.camera.ScreenPointToRay(Input.mousePosition);        
         rotationPlane = new Plane(transform.up, transform.position);        
         var enter : float;
         if(rotationPlane.Raycast(ray , enter)){
             firstHitPos = ray.GetPoint(enter);
         }
 }
 
 
 function OnMouseDrag(){
     var pos = getPlanePos();    
     var tmpPos = Vector3(pos.x, 0, pos.z);    
     
     //var rotationStart= Quaternion.LookRotation(Vector3(firstHitPos.x, 0, firstHitPos.z), transform.up);    
     var rotationStart= transform.rotation;    
     var rotationEnd = Quaternion.LookRotation(tmpPos, transform.up);
     transform.rotation = Quaternion.Lerp ( rotationStart , rotationEnd, Time.time * speed);
 }
 
 function getPlanePos(){
 
     var mousePos = Input.mousePosition;
     var ray = mainCam.camera.ScreenPointToRay(mousePos);
     var mouseWorldPos = mainCam.camera.ScreenToWorldPoint(Input.mousePosition);
     var enter : float;
     
     if(rotationPlane.Raycast(ray , enter)){
         var returnVal = mainCam.transform.position + ray.direction.normalized * enter;        
         return (returnVal); 
     
     }
 }


ask-unity-gnome.jpg (83.7 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Jan 24, 2013 at 03:16 AM

There are a number of problems with the code above. The first is how you are handling your Quaternion.Lerp. Lerp is for rotation over time. You set the start and end rotations, then you pass a fraction between 0 and 1 for where along that rotation you want to be. Typically you do t$$anonymous$$s over time. You are passing Time.time. T$$anonymous$$s is seen frequently in coding examples, but really it is only using the fractional amount in the number of seconds. Your starting point when you call your Lerp for the first time is unknown. And you are resetting the start rotation at every update. Plus I don't t$$anonymous$$nk you need or want to do a Lerp in t$$anonymous$$s situation.

You may have special needs with your rotation, but if all you want to do is tie mouse movement to rotation, you can use somet$$anonymous$$ng simpler like:

 private var factor : float = 0.6;
 private var v3StartPos : Vector3;
 private var v3StartRot : Vector3;
 
 function OnMouseDown(){
        v3StartPos = Input.mousePosition;
        v3StartRot = transform.eulerAngles;
 }
 
 function OnMouseDrag(){
     var v3T : Vector3 = Input.mousePosition;
 
     transform.eulerAngles = v3StartRot + Vector3.up * (v3StartPos - v3T).x * factor;
 }

I've used a very basic example as a start. T$$anonymous$$s code uses screen units, so you would need to make some additions if you want to run on multiple resolutions and map the same mouse movement as a percentage of the screen to the same rotation.

Comment
Add comment · Show 8 · 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 Paul Graystone · Jan 24, 2013 at 10:14 AM 0
Share

Thank you very much for your solution suggested solution. Your code works really well. But in order to rotate I have to push my mouse from left to right. I want to have a real-word rotation, so that I have to do circle my mouse in order to rotate. Please check my edited question for further details. Thank you!

avatar image robertbu · Jan 24, 2013 at 01:54 PM 0
Share

Ahhh. I really missed the boat with my answer. A quick and dirty solution is to use the DragRigidbody script from standard assets. Attach a Rigidbody, turn off Use Gravity and freeze everything by y rotation. I'll take another look at your code for the circle rotation when I have a bit more time.

avatar image robertbu · Jan 24, 2013 at 02:18 PM 1
Share

And here is another script that may be what you are looking for:

 private var v3StartRot : Vector3;
 private var v2Axis     : Vector2;
 private var fStartAngle : float;
 
 function OnMouseDown(){
    v3StartRot = transform.eulerAngles;
    v2Axis = Camera.main.WorldToScreenPoint(transform.position);
    var v2T : Vector2 = Input.mousePosition - v2Axis;
    fStartAngle = Mathf.Atan2(v2T.y, v2T.x);
 }
 
 function OnMouseDrag(){
     var v2T : Vector2 = Input.mousePosition - v2Axis;
     var fAngle = Mathf.Atan2(v2T.y, v2T.x);
     var v3T : Vector3 = v3StartRot;
     v3T.y = v3T.y - (fAngle - fStartAngle) * Mathf.Rad2Deg;
     transform.eulerAngles = v3T;
 }
avatar image Paul Graystone · Jan 24, 2013 at 09:31 PM 0
Share

Brilliant! This code works as wished! Is there a way to make the rotation in local space? Meaning the object is rotating about its local y-axis?

avatar image robertbu · Jan 24, 2013 at 10:13 PM 0
Share

I don't have time to test it be sure, but I think you can just replace the two instances of 'eulerAngles' with 'localEulerAngles'.

Show more comments

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 People are following this question.

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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Why does only the first of sequential quaternion operations work? 1 Answer

Workaround for Quaternion.eulerAngles 360 Degree Limit? 1 Answer

[SOLVED]Possible Alternation of Transform.Rotate 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