transform.EulerAngles绕圈问题

时间:2013-06-01 19:39:08

标签: unity3d

所以我有一个扁平的圆圈,当我使用Touch脚本点击它时,我可以旋转它。但是,如果我最终触摸圆圈的中心,它将自动翻转到相反的方向。它有点不稳定。我认为这最终是我的数学技能,但是如果有人可以看一下会很感激。

float changeY = 0;
    // touch position
    Vector3 pos = touch.worldPos;
    Vector2 curLineEnd = new Vector2(pos[0], pos[2]);
    float curLineDistance = GetDistance(new Vector2(lineStart[0], lineStart[1]), curLineEnd);

    if (curLineDistance < edgeWidth) {
        return;
    }

    if(curLineDistance < 0.6)
    {
        float angle = GetAngle(curLineEnd, startLineEnd);

        if (angle > lastAngle+2) 
        {
            changeY += 5;
        }

        else if (angle < lastAngle-2) 
        {
            changeY -= 5;
        }

        lastAngle = angle;
        Debug.Log(angle);

        //transform.eulerAngles = new Vector3(270, angle + lastY, 0);
        transform.Rotate(new Vector3(0,changeY),Space.World);
    }






    public float GetAngle(Vector2 lineOneEnd, Vector2 lineTwoEnd)
{
    float a = lineOneEnd[0] - lineStart[0];
    float b = lineOneEnd[1] - lineStart[1];
    float c = lineTwoEnd[0] - lineStart[0];
    float d = lineTwoEnd[1] - lineStart[1];
    float lineOneTan = Mathf.Atan2(a, b);
    float lineTwoTan = Mathf.Atan2(c, d);
    float angle = (lineOneTan - lineTwoTan) * 180 / Mathf.PI;
    return angle;
}    

0 个答案:

没有答案