簡體   English   中英

為什么我的 Unity object 旋轉受限?

[英]Why is my Unity object rotation being limited?

public class cameraRotation : MonoBehaviour
{
    [SerializeField] float speed = 20f;
    public Vector2 movement;
    public float lrCMovement;
    public float udCMovement;
    [SerializeField] float rotationX;
    [SerializeField] float rotationY;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    public void onCameraMove(InputAction.CallbackContext ctx)
    {

        movement = ctx.ReadValue<Vector2>() * speed;
        lrCMovement = movement.x;
        udCMovement = movement.y;
     

    }
    // Update is called once per frame
    void Update()
    {
        transform.rotation = new Quaternion(transform.rotation.x + -udCMovement *   
        Time.fixedDeltaTime, transform.rotation.y + lrCMovement * Time.fixedDeltaTime, 
        transform.rotation.z, 1);

    }
}

我制作了一個 object,我希望它通過游戲手柄桿根據用戶輸入進行旋轉。 但是,當桿向一側傾斜更長時間時,它不會旋轉 360 度,而是在大約 40 度處停止。 有誰知道我做錯了什么?

首先, Quaternion並不比末尾為 1 的向量復雜很多,因此您不能只添加到 x、y 和 z。 但是你可以這樣做

transform.rotation *= Quaternion.Euler(-udCMovement *   
        Time.fixedDeltaTime, lrCMovement * Time.fixedDeltaTime, 0);

因為您使用Time.fixedDeltaTime我建議您使用FixedUpate或使用Time.deltaTime並且不要錯過並匹配它們。

我懷疑這是萬向節鎖定的情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM