- Home /
Quaternion.FromToRotation misunderstanding
I am trying to make an f-zero type game where my ship stays parallel to the ground. I would expect the following code to align the ships up vector to the normal projecting from the surface (I am casting a ray straight down from the ship). Judging from the Debug log though, this isn't happening. Anyone know how I can get the desired result?
Quaternion rot = Quaternion.FromToRotation(transform.up, hit.normal);
Debug.Log("myup " + transform.up + " hitnormal " + hit.normal );
transform.rotation = Quaternion.RotateTowards(transform.rotation, rot, Time.deltaTime*10);
unityGE$$anonymous$$S.com for very good articles on Quaternions
Answer by Owen-Reynolds · Feb 07, 2013 at 04:38 PM
FromToRotation is the difference between two rotations. So, you would gradually add it to your current rotation. Your code just changes to it.
For example, simplifying, suppose your Up is 90 degrees and hit.normal is only 80 degrees. FromTo gives you 10. You don't want to be 10, you want to add 10.
Can compute 1 frame worth of rot and add that [tested]:
rot = Quaternion.RotateTowards(Quaternion.identity, rot, Time.deltaTime*10);
transform.rotation = rot * transform.rotation;
NOTE: multiplying Quats is different in either order. The 2nd (I think) uses the local axis of the 1st. So "add rot to your rotation" isn't as easy as it sounds. Sometimes can just trial&error the order. Sort of makes sense rot is 1st, since it was in world coords.
Answer by andrew-lukasik · Jan 22, 2016 at 04:00 PM
(Z+ is forward) Solution:
Quaternion NEW_ROTATION = Quaternion.LookRotation(
Vector3.ProjectOnPlane( LOOK_AT_DIRECTION , SURFACE_NORMAL) ,
SURFACE_NORMAL
);
transform.rotation = NEW_ROTATION;
this fixed my problem of the gameobject i tried to align with a wall showing different up rotations depending which angle the camera looked at it from. thanks