- Home /
Rigidbody spins out of control when colliding into corner
So, as the title says, With my movement/rotation script (a modified version of Unity's Survival Tutorial), this is the result of running into the corner of the room, but it also happens with collisions on other objects. Since this is affecting the rotation, I think it has something to do with the part of my script that turns the player. I have the target rotation of the Slerp to be what axis the player is inputting. I can stop the rotation by moving around again, but as soon as the moving stops it goes back to its habit of spinning.
void FixedUpdate ()
{
// Store the input axes.
float h = Input.GetAxisRaw ("Horizontal");
float v = Input.GetAxisRaw ("Vertical");
// Set the movement vector based on the axis input.
movement.Set (h, 0f, v);
// Normalise the movement vector and make it proportional to the speed per second.
movement = movement.normalized * speed * Time.deltaTime;
// Move the player to it's current position plus the movement.
rb.velocity = new Vector3(h * speed, 0.0f, v * speed);
// Turn the player.
Vector3 moveDirection = new Vector3 (h, 0f, v);
//Check if there is input
if (moveDirection != Vector3.zero)
{
Quaternion rotationTarget = Quaternion.LookRotation(moveDirection);
rb.transform.rotation = Quaternion.Slerp(rb.transform.rotation, rotationTarget, Time.deltaTime * rotSpeed);
}
}
ezgifcom-resize.gif
(376.4 kB)
Comment
When you say "check if there is input", try to make it less sensitive. Ins$$anonymous$$d of needing to be zero, try doing if(moveDirection.sqr$$anonymous$$agnitude > someBufferLikeOne)