一、Unity里的向量、角度操作
一、已知两向量A, B ,求出A->B的旋转角旧版本:
public static float SignedAngleBetween(Vector3 a, Vector3 b, Vector3 n){ float angle = Vector3.Angle(a,b); float sign = Mathf.Sign(Vector3.Dot(n,Vector3.Cross(a,b))); float signed_angle = angle * sign; return (signed_angle <= 0) ? 360 + signed_angle : signed_angle;}
例子:已知向量a=(1, 0, 1), b=(-1, 0, 1) ,求a顺时针转到b的旋转角,则n应该为(0, 1, 0),结果为270度。若n=(0, -1, 0),则逆时针,结果为90度。
新版本,Unity已提供现成接口:
float angle = Vector2.SignedAngle(atkDir, new Vector2(0, 1));
float angle = Vector3.SignedAngle(atkDir, new Vector3(0, 1,0));
页:
[1]