• 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 YamiZee · Apr 24, 2019 at 09:51 AM · rotationquaternionfromtorotation

How do I get this FromToRotation code to work properly?

My code has a custom variable called gravity. The player is supposed to align with this gravity so that the players -transform.up == gravity.normalized. Instead the player object spins around like crazy.

 if (-transform.up != gravity.normalized) {
     transform.rotation *= Quaternion.FromToRotation(-transform.up, gravity.normalized);
     Debug.Log(-transform.up); //should print gravity.normalized but does not
 }

(Note: the player is not guaranteed to be upright, the rotation changes throughout the game. so maybe theres an issue with global vs local rotation that i don't understand..?)

Comment
Add comment · Show 2
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 xxmariofer · Apr 24, 2019 at 12:00 PM 0
Share

why are you multiplying? try

 transform.rotation = Quaternion.FromToRotation(-transform.up, gravity.normalized);
avatar image Hellium · Apr 24, 2019 at 12:09 PM 1
Share

a *= b, can be "translated" as a = a * b. However, the quaternion product is not commutative!

I am not an expert with quaternion, but I think what you want to do is:

 transform.rotation = Quaternion.FromToRotation(-transform.up, gravity.normalized) * transform.rotation;

1 Reply

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

Answer by YamiZee · Apr 24, 2019 at 01:00 PM

The solution was to change to order. Less pretty this way but more functional. Thanks to Hellium for pointing this out.

 transform.rotation = Quaternion.FromToRotation(-transform.up, gravity) * transform.rotation;
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

140 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

Related Questions

Doing 2D only rotation using FromToRotation + RotateTowards 0 Answers

Rotation, my script shouldn't work but does. 0 Answers

Rotate object with quaternion to direction points 1 Answer

Rotate a sphere by its vertices... 1 Answer

Need help on making a semi-circle rotation 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges