Version: 2022.1
LanguageEnglish
  • C#

Vector3.SignedAngle

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public static float SignedAngle(Vector3 from, Vector3 to, Vector3 axis);

Parameters

from The vector from which the angular difference is measured.
to The vector to which the angular difference is measured.
axis A vector around which the other vectors are rotated.

Returns

float Returns the signed angle between from and to in degrees.

Description

Calculates the signed angle between vectors from and to in relation to axis.

The angle returned is the angle of rotation from the first vector to the second, when treating these first two vector inputs as directions. These two vectors also define the plane of rotation, meaning they are parallel to the plane. This means the axis of rotation around which the angle is calculated is the cross product of the first and second vectors (and not the 3rd "axis" parameter). You can use the "left hand rule" to determine the axis of rotation, given the two input vectors. The third input (named the “axis” parameter), gives you a way to provide a contextual direction to include in the calculation. This has the result of flipping the sign of the result depending on whether this third vector that you supply falls above or below the plane of rotation defined by the first two input vectors. Therefore the sign of the final result depends on two things: the order in which you supply the "from" and "to" vector, and the direction of the third "axis" vector.
Note: The angle returned will always be between -180 and 180 degrees, because the method returns the smallest angle between the vectors. That is, it will never return a reflex angle.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public Transform target;

void Update() { Vector3 targetDir = target.position - transform.position; Vector3 forward = transform.forward; float angle = Vector3.SignedAngle(targetDir, forward, Vector3.up); if (angle < -5.0F) print("turn left"); else if (angle > 5.0F) print("turn right"); else print("forward"); } }

See Also: Angle function.