• 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 larsonmattr · Oct 16, 2017 at 02:41 AM · rotationtransformdrag

Trackball-style movement of object?

My goal is to move an object's rotation or localRotation like a trackball, where subsequent movements left/right rotate the object to the left or right (around Z-axis) and subsequent movements up/down rotate the object up or down (around X-axis). The code sample below describes how I am trying to do this. (GestureManager is an input from the HoloLens for dragging, but similar to be done with mouse drag events).

             float rotationFactorY = GestureManager.Instance.NavigationPosition.y * RotationSensitivity;
             transform.rotation = transform.rotation * Quaternion.AngleAxis (-1 * rotationFactorY, Vector3.right); 

             float rotationFactorX = GestureManager.Instance.NavigationPosition.x * RotationSensitivity;
             transform.rotation = transform.rotation * Quaternion.AngleAxis (-1 * rotationFactorX, Vector3.up); 

 

Instead, what happens is that at first, the rotation can move as expected from the perspective of the user's view. But after moving the object 90 degrees around the Z axis, movements around the X axis no longer behave. Instead of spinning the object up/down from the perspective of the user, it is now rotating the object around the Y axis, instead.

How do I apply rotations based on X/Y drags (2D space) to rotate an object reliably from the perspective of the view, not from the coordinate space of the object?

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
2
Best Answer

Answer by Bunny83 · Oct 16, 2017 at 03:13 AM

You have to reverse the multiplication

 transform.rotation = Quaternion.AngleAxis ( -rotationFactorY, Vector3.right) * transform.rotation; 
 transform.rotation = Quaternion.AngleAxis ( -rotationFactorX, Vector3.up) * transform.rotation; 

Quaternion multiplication is not commutative. The quaternion on the left is actually rotating the quaternion on the right. If you reverse it you actually rotate your angleAxis by the current rotation.

Quaternions explained

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 larsonmattr · Oct 18, 2017 at 03:23 AM 0
Share

Thank you for indicating that the main problem was the multiplication order of the two Quaternions. Another issue is that the Vector3.right needs to be replaced by a 'right' vector from the camera's coordinate system. That lets you move around the object, but still be able to rotate it correctly.

             float rotationFactorY = Gesture$$anonymous$$anager.Instance.NavigationPosition.y * RotationSensitivity;
             transform.rotation = Quaternion.AngleAxis (1 * rotationFactorY, Camera.main.transform.right) * transform.rotation; 

             float rotationFactorX = Gesture$$anonymous$$anager.Instance.NavigationPosition.x * RotationSensitivity;
             transform.rotation = Quaternion.AngleAxis (-1 * rotationFactorX, Camera.main.transform.up) * transform.rotation; 

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

110 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Top-down character running animation based on facing direction 0 Answers

Syncing Position and Rotation data in ECS tests? 0 Answers

How to make Camera position Independent of its Rotation? 1 Answer

Rotating Camera around player object 0 Answers

How do you make a player move on a remote items local axis? 0 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