- Home /
LookRotation up vector not working?
I'm trying to give an object a rotation with a facing and an up vector, but when I use the code below, nothing seems to happen to the rotation of the object. I expect the result to be the the object basically looked down at a 45 degree angle since the up vector is set to be 45 degrees. But instead, the object's up vector remains pointing straight up. Why doesn't this work?
gameObject.transform.rotation = Quaternion.LookRotation(Vector3.forward, Vector3.forward + Vector3.up);
Answer by CodesCove · Nov 16, 2021 at 06:07 AM
You get this to work ok by defining it like this
this.gameObject.transform.rotation = Quaternion.LookRotation(Vector3.forward - Vector3.up, Vector3.up);
According to the manual the direction of Z axis will be aligned according to the 1st parameter and X axis alignment will be calculated as cross product with Z and up-reference direction. After that the Y-axis will be aligned with cross btw. Z and X.
So in other terms:
When you set the 1st parameter as object direction (yaw and pitch) it will be just that. You cannot change it with the second parameter. What you change with the second parameter is objects roll. So the 2nd parameter is only used to set the rotation around the objects forward axis. Since the operations performed are not commutative, the order of the parameters is important.
Exactly, well explained :)
Though in order to look down you have to subtract up from forward, otherwise you will be looking up.