簡體   English   中英

C# Unity 中的旋轉優先級

[英]Rotation Priority in C# Unity

public float turnSmoothTimeFire = 0.0001f;
float turnSmoothVelocity;
     
void Update()
    {
            float horizontal = Input.GetAxisRaw("Horizontal");
            float vertical = Input.GetAxisRaw("Vertical");
            Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;

    if (Input.GetButton("Fire1"))
    {
        float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
        float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTimeFire);
        transform.rotation = Quaternion.Euler(0f, angle, 0f);
    }
}

我有另一個設置完全相同的 if 語句(只是變量的調用方式不同),用於將我的玩家轉向移動方向,我想知道我是否可以優先考慮我的旋轉。 目前,我的玩家寧願轉向我移動的方向而不是我開火的方向,我希望它反過來。 非常感謝您的幫助。

您可以先進行Fire檢查,然后在檢查移動邏輯之前從方法返回。

另一種方法是通過添加為移動計算的角度和用於瞄准的角度來考慮兩者。

這取決於您的要求。

您還可以檢查Transform.Rotate方法,建議使用該方法代替手動旋轉設置。

暫無
暫無

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

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