Version: 2017.1
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.

Description

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.

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.

Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961