繁体   English   中英

Unity 2D旋转不顺畅

[英]Unity 2D rotation not smooth

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

没错,右边的部分。 问题出在左侧。 右侧的转弯是平滑的。 左侧的转向角度不流畅,平滑

观察问题: 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;

        }

    }

}

在这个声明中:

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM