Select your preferred scripting language. All code snippets will be displayed in this language.
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.
CloseFor 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.
Closefrom | 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 the signed angle in degrees between from
and to
.
The smaller of the two possible angles between the two vectors is returned, therefore the result will never be greater than 180 degrees or smaller than -180 degrees.
If you imagine the from and to vectors as lines on a piece of paper, both originating from the same point, then the axis
vector would point up out of the paper.
The measured angle between the two vectors would be positive in a clockwise direction and negative in an anti-clockwise direction.
#pragma strict public var target: Transform; function Update() { var targetDir: Vector3 = target.position - transform.position; var forward: Vector3 = transform.forward; var angle: float = Vector3.SignedAngle(targetDir, forward, Vector3.up); if (angle < -5.0F) print("turn left"); elseif (angle > 5.0F) print("turn right"); else print("forward"); }
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.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information