Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Dakcenturi · Oct 05, 2012 at 02:52 PM · rotationmousequaternionrotatengui

How do I rotate Z axis based off mouse click and drag?

I am attempting to rotate an object and it is just spinning out of control. I know it boils down to something with my math not being correct but I'm still trying to wrap my head around the whole Vector/Quaternion thing.

Any ideas as to why it is spinning so out of control. Also any suggestions on good resources to better understand Vectors and Quaternion?

 public class SpinWithMouse : MonoBehaviour
 {
     public Transform target;
     public float speed = 1f;
 
     Transform mTrans;
     Vector3 startPoint;
 
     void Start ()
     {
         mTrans = transform;
     }
     
     void OnPress()
     {
         Vector3 currentLoc = UICamera.currentTouch.pos;
         startPoint = (currentLoc - transform.localPosition);
     }
 
     void OnDrag (Vector2 delta)
     {
         Vector3 newLoc = UICamera.currentTouch.pos;
         Vector3 targetPoint = (newLoc - transform.localPosition);
         
         Quaternion targetRotation = Quaternion.LookRotation(newLoc - startPoint);
         
         if (target != null)
         {
             target.localRotation = Quaternion.Slerp(target.localRotation, targetRotation, speed) * target.localRotation;
         }
         else
         {
             mTrans.localRotation = Quaternion.Slerp(mTrans.localRotation, targetRotation, speed) * mTrans.localRotation;
         }
     }
 }
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 Dakcenturi · Oct 12, 2012 at 08:37 PM 0
Share

Anyone have any suggestions or thoughts? I've tried a number of different things and I just can't seem to get this working correctly. I know it has to be something with my math, but I can't wrap my head around where the problem is.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Oct 05, 2012 at 02:54 PM

You are multiplying speed by Time.time in the drag - that's not a good idea. Slerp wants a number between 0 and 1, but Time.time is the number of seconds since the game started.

Your best choice would be to replace that with something like speed/10 which will incrementally move the rotation towards the target each frame by a percentage of the difference which will give a smoothed effect.

Comment
Add comment · Show 1 · 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 Dakcenturi · Oct 08, 2012 at 02:24 PM 0
Share

Ok, I changed the code some and I got the crazy spining under control however I need it to rotate on the Z-axis.

using UnityEngine;

public class SpinWith$$anonymous$$ouse : $$anonymous$$onoBehaviour { public Transform target; public float speed = 1f;

 Transform mTrans;
 Vector3 startPoint;

 void Start ()
 {
     mTrans = transform;
 }
 
 void OnPress()
 {
     Vector3 currentLoc = UICamera.currentTouch.pos;
     startPoint = (currentLoc - transform.localPosition);
 }

 void OnDrag (Vector2 delta)
 {
     Vector3 newLoc = UICamera.currentTouch.pos;
     Vector3 targetPoint = (newLoc - transform.localPosition);
     
     Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.localPosition);
     
     if (target != null)
     {
         target.localRotation = Quaternion.Slerp(target.localRotation, targetRotation, speed);
     }
     else
     {
         mTrans.localRotation = Quaternion.Slerp(mTrans.localRotation, targetRotation, speed);
     }
 }

}

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.

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

Rotation Jumping values (0 to 180) 1 Answer

How can I move an object to click point in 2D? 0 Answers

Why Rotate player toward mouse direction keeps increasing rotation speed? 1 Answer

How can you make a nav mesh agent rotate 180 degrees? 1 Answer

When applying a 90 degree rotation to Euler Angles, it is over/undershooting sometimes.. 2 Answers


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