- Home /
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..?)
why are you multiplying? try
transform.rotation = Quaternion.FromToRotation(-transform.up, gravity.normalized);
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;
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;
Your answer
Follow this Question
Related Questions
Doing 2D only rotation using FromToRotation + RotateTowards 0 Answers
Rotation, my script shouldn't work but does. 0 Answers
Rotate a sphere by its vertices... 1 Answer
Rotate object with quaternion to direction points 1 Answer
Check if rotation (only on Y axis) of some object toward another is finshed? 0 Answers