方法一:
鼠标控制物体前后左右移动,上下的Y值固定; 限制齿轮的旋转圈数(360*3),要先计算两个向量的角度(没有考虑两个向量转的读数大于180的情况,因为理论上不太可能一帧转那么多,以后再说吧),然后再判断两个向量是顺时针还是逆时针,最后计算出上一帧的角度和目标角度之间的差值,最后,用上一帧的角度加上插值,得出目标角度。虽然麻烦,但是可以根据 差值,累加旋转的角度,不会受到顺逆时针的影响。
public bool left = false;
public void OnDrag(PointerEventData eventData)
{
var oldDir = moveBackPos - transform.position;
var nowDir = Input.mousePosition - transform.position;
var an = Vector3.Angle(oldDir, nowDir);
var cross = oldDir.x * nowDir.y - oldDir.y * nowDir.x;
an = cross > 0 ? an : -an;
angeleCount += an;
if (left)
{
if (angeleCount <= 360 * 3 && angeleCount >= 0)
{
transform.localEulerAngles = new Vector3(0, 0, lastAngle + an);
}
}
else
{
if (angeleCount >= -360 * 3 && angeleCount <= 0)
{
transform.localEulerAngles = new Vector3(0, 0, lastAngle + an);
}
}
moveBackPos = Input.mousePosition;
lastAngle = transform.localEulerAngles.z;
}
|