Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 RichardRacz · Apr 27, 2020 at 11:25 PM · rotationquaternionlookrotationfromtorotation

Rotate object with quaternion to direction points

Hi All, I try to rotate an object which is in the center of a cube toward the cube's vertices using a virtual reality controller (if that object's forward direction is 'almost towards' to the vertex). I've tried:

         Quaternion lookRotation = Quaternion.LookRotation(dirTo);
         gameObject.transform.rotation = lookRotation;

and I've tried multiply with a defined rotation uses

         Quaternion fromToRotation = Quaternion.FromToRotation( gameObject.transform.forward, dirTo);
         gameObject.transform.rotation *= fromToRotation;

but both have the problem: when I rotate the object toward 'global y (up)' from the original direction, it rotates an additional 90 degree around the y-axle (in all other cases it works well). What is wrong with my solution or is there a simple method to avoid this behavior, or what is the best practice for this kind of problem? Thanks in advance!

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

Answer by Bunny83 · Apr 28, 2020 at 12:31 AM

Well your issue is that Quaternion.LookRotation does not take one argument but two. However the second argument is optional and when not manually provided it defaults to Vector3.up. This upvector is used as a "hint" how the rotation should be rotated around your provided forward vector. In most cases using just Vector3.up gives a reasonable result. However if your forward axis points along the up vector (up or down) the orientation around that axis can not be determined.


Your approach with FromToRotation should work. However it seems a bit strange that you rotate the current "gameobject" but you use the forward direction of that "m_controlledObject". Are the two objects related in any way?


Note that free rotation in 3d space can result to some odd results. Rotating around an arbitrary axis could change the orientation completely so you might end up with a slight roll around z as well. In many cases we want to keep the orientation somewhat parallel to the ground. Think of a usual FPS / camera setup. You can rotate 360° around y but only +-90 on the local x axis. The z rotation usually stays 0.


Quaternions can perform any arbitrary rotation in 3d space. If you just calculate the rotation between two directions you get a rotation that rotates from that direction directly to the other direction. The axis of rotation will be perpendicular to the plane the two vectors lie in. This is essentially what FromToRotation does.

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.

Follow this Question

Answers Answers and Comments

165 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 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

Aligning an object to both the surface normal and the input direction at the same time 0 Answers

Basic AI Locked Axis 1 Answer

LookRotation return weird values 1 Answer

How do I get this FromToRotation code to work properly? 1 Answer

More specific Quaternion.LookRotation 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