我已經開始制作 3d 游戲,并且我已經設定了我的播放器、墻壁物件和地板。每當我的播放器物件接觸墻壁時,它就會像向后倒一樣旋轉并且永遠繼續旋轉。
我曾嘗試在玩家的剛體上啟用 y 和 z 的凍結旋轉(顯然需要 x 旋轉,但這不是問題),并嘗試了幾種不同的方法,使墻保持靜止。將其更改為運動學等,但我找不到在接觸墻壁后阻止播放器旋轉的方法。
玩家移動在固定更新中控制,如圖所示:
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
m_Movement.Set(horizontal, 0f, vertical);
m_Movement.Normalize();
bool hasHorizontalInput = !Mathf.Approximately(horizontal, 0f);
bool hasVerticalInput = !Mathf.Approximately(vertical, 0f);
bool isWalking = hasHorizontalInput || hasVerticalInput;
bool isShiftKeyDown = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
if (hasHorizontalInput || hasVerticalInput)
{
if (isShiftKeyDown)
{
m_Animator.SetBool("IsRunning", true);
m_Animator.SetBool("IsWalking", false);
m_Animator.SetBool("IsIdle", false);
m_AudioSource.clip = running;
} else
{
m_Animator.SetBool("IsRunning", false);
m_Animator.SetBool("IsWalking", true);
m_Animator.SetBool("IsIdle", false);
m_AudioSource.clip = walking;
}
} else
{
m_Animator.SetBool("IsRunning", false);
m_Animator.SetBool("IsWalking", false);
m_Animator.SetBool("IsIdle", true);
}
//m_Animator.SetBool("IsWalking", isWalking);
Vector3 desiredForward = Vector3.RotateTowards(transform.forward, m_Movement, turnSpeed * Time.deltaTime, 0f);
m_Rotation = Quaternion.LookRotation(desiredForward);
if (isWalking)
{
if (!m_AudioSource.isPlaying)
{
m_AudioSource.Play();
}
}
else
{
m_AudioSource.Stop();
}
}```
uj5u.com熱心網友回復:
嘗試增加玩家剛體中的 Angular Drag。
uj5u.com熱心網友回復:
我相信問題可能出在以下幾行:
Vector3 desiredForward = Vector3.RotateTowards(transform.forward, m_Movement, turnSpeed * Time.deltaTime, 0f);
m_Rotation = Quaternion.LookRotation(desiredForward);
在這里,您正在transform.forward向旋轉m_Movement,但如果我了解您的代碼如何正確作業,這將導致它永遠旋轉。嘗試洗掉這些行,看看會發生什么。另外,它在哪個軸上旋轉?
uj5u.com熱心網友回復:
如果其他人有這個問題,我發現解決這個問題的方法是在 FixedUpdate 內部,在使用輸入進行任何呼叫之前,將 Rigidbody.velocity 設定為 0。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/393337.html
標籤:统一3d
