簡體   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