Vector3.Angle

计算两向量间角度

定义

public static float Angle(Vector3 from, Vector3 to);
参数名类型必要说明
fromVector3源向量
toVector3目标向量

示例

using UnityEngine;

public class ExampleScript : MonoBehaviour
{
    public Transform target;

    private bool IsAiming()
    {
        var playerTransform = transform;
        var targetDir = target.position - playerTransform.position;
        var angle = Vector3.Angle(targetDir, playerTransform.forward);
        return angle < 5.0f;
    }
}