Unity 关于三角函数ATan计算值不对或与预期不符的情况解析

注意:

1. Mathf.Atan 计算的结果为弧度值,不是角度

2. 通过Atan计算弧度时,点在平面坐标系运动到二三象限会出现翻转的情况

观察tan函数曲线:

Unity 关于三角函数ATan计算值不对或与预期不符的情况解析_第1张图片

对照平面,二三象限内值会跳跃到X轴另一侧:

Unity 关于三角函数ATan计算值不对或与预期不符的情况解析_第2张图片

代码:

//不动点
Vector3 pointBase;
//运动点
Vector3 pointMove;

//弧度
float radian = Mathf.Atan((pointMove.localPosition.y - pointBase.localPosition.y) / (pointMove.localPosition.x - pointBase.localPosition.x));
//角度
float angle = radian / Mathf.PI * 180;

//二三象限点角度翻转
if ((pointMove.localPosition.x - pointBase.localPosition.x) < 0)
{
    angle = angle + 180;
}

这样得到的角度能与预期匹配。

 

 

你可能感兴趣的:(Unity经验,unity3d,三角函数)