Unity 2D旋转不流畅

时间:2019-04-26 06:18:01

标签: c# unity3d rotation

请勿在左角旋转“流畅,流畅” 。左角转动但不流畅且不光滑。 角度之间没有角度

在正确的部分没有问题。问题在左侧。右侧的转弯很平滑。左侧的转角不是流畅,平滑

注意问题: https://youtu.be/kuBWoF5r2Bs

Gun Sprite C#代码:

 public GameObject projectile;

    public Camera cam;

    void FixedUpdate()
    {
        //rotation
        Vector3 mousePos = Input.mousePosition;
        Vector3 objectPos = cam.WorldToScreenPoint(transform.position);

        mousePos.x = mousePos.x - objectPos.x;
        mousePos.y = mousePos.y - objectPos.y;

        float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
        //transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle)); //Rotating!

        if (angle > 0f && angle < 100f || angle < 0f && angle > -90f)
        {

            //Reverse
            Vector3 theScale = transform.localScale;
            theScale.x = 1;
            transform.localScale = theScale;

            //Limit
            angle = Mathf.Clamp(angle, -24, 24);
            Quaternion target = Quaternion.Euler(new Vector3(0, 0, angle));
            transform.rotation = target;

        }

        if (angle > 100f && angle < 180f || angle < -90f && angle > -180f)
        {

            //Reverse
            Vector3 theScale = transform.localScale;
            theScale.x = -1;
            transform.localScale = theScale;

            //Limit
            angle = Mathf.Clamp(-angle, -24, 24);
            Quaternion target = Quaternion.Euler(new Vector3(0, 0, angle));
            transform.rotation = target;

        }

    }

}

1 个答案:

答案 0 :(得分:0)

在此声明中:

if (angle > 100f && angle < 180f || angle < -90f && angle > -180f)

唯一通过该角度的角度是那些大于100或小于-90的角度。第一个范围内的所有数字均大于25,第二个范围内的所有数字均小于-25 ....,因此该值将始终变为25或-25。

在第二条语句中,尝试从角度添加或减去某些内容,直到获得所需范围内的值。也许是这样的:

if (angle > 100) angle -= 100;
if (angle < -90) angle += 90;