Description
Creates a rotation with the specified
forward
andupwards
directions.Z axis will be aligned with
forward
, X axis aligned with cross product betweenforward
andupwards
, and Y axis aligned with cross product between Z and X.
Returns identity ifforward
orupwards
magnitude is zero.
Returns identity ifforward
andupwards
are colinear.https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html
void LookTargetMovementDirection(Vector3 targetPosition, Vector3 targetVelocity)
{
Vector3 targetLookRotation = (targetPosition + targetVelocity) - targetPosition;
if(targetLookRotation != Vector3.zero){
transform.rotation = Quaternion.LookRotation(targetLookRotation);
} else {
Debug.Log("targetPosition = " + targetPosition);
Debug.Log("targetVelocity = " + targetVelocity);
Debug.Log("targetLookRotation = " + targetLookRotation);
}
}
The vector3 parameter we were giving Quaternion.LookRotation(..) has been stored in a Vector3 value instead so a comparison to Vector3.zero can be ran, this will mean our Debug.Log(..) calls will only happen in instances where the look rotation viewing vector would be zero.
With this debugging we should be able to see in the console which variable is causing the vector3 to be zero, in this example it would be caused by the targetVelocity being zero and we can choose to either not update the rotation while that is zero or restructure our code in a different way depending on the situation.
https://www.unity3dtips.com/look-rotation-viewing-vector-is-zero-unity-error/