I have started making a 3d game, and I have my player and a wall object and a floor set up. Whenever my player object touches the wall though it rotates as though falling backwards and just continues to rotate forever.
I have tried enabling freeze rotation of both the y and the z on the rigidbody of the player (x rotation is obviously needed and is not the problem) and have tried a couple of different things with either making the wall static. changing it to kinematic etc and I cannot find a way to stop the player rotating after touching the wall.
Player movement is controlled in fixed update as shown:
{
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();
}
}```
CodePudding user response:
Try to increase the Angular Drag in the rigidbody of the player.
CodePudding user response:
I belive the problem might be in the these lines:
Vector3 desiredForward = Vector3.RotateTowards(transform.forward, m_Movement, turnSpeed * Time.deltaTime, 0f);
m_Rotation = Quaternion.LookRotation(desiredForward);
Here you are rotating transform.forward
towards m_Movement
, but if i understand how your code works correctly, this will cause it to spin forever. Try removing these lines and see what happens. Also, on which axis is it spinning?