- Home /
Finding the difference between two directions?
I'm using the included third-person character controller. It has a script that rotates the player character at a set speed between two directions, but changes direction instantly if the character is moving slowly.
if (targetDirection != Vector3.zero)
{
// If we are really slow, just snap to the target direction
if (moveSpeed < walkSpeed * 0.9 && grounded)
{
moveDirection = targetDirection.normalized;
}
// Otherwise smoothly turn towards it
else
{
moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
moveDirection = moveDirection.normalized;
}
}
I want my character to quickly pivot if the player makes a fast 180 degree change of direction. My problem comes from being new to Java and 3D in general. I need to find the difference between the targetDirection and the current moveDirection in terms of degrees, then check to see if the absolute value is greater than, say, 150. If it is, I'll run my pivot code (no worries there).
The Y axis isn't important for this, since it can only occur when the player is grounded.
Thanks a lot for your help. I'm loving Unity so far.
Vector3.Angle will give you the angle between 2 vectors between 0 and 180 degrees.