- Home /
How to rotate player rigidbody ?
How do I rotate the player rigidbody with a free look camera, when i move around? I'm using C#
Answer by Jonas47 · Jul 24, 2021 at 11:44 AM
Rigidbody.freezeRotation, RigidBody.Sleep() and Rigidbody.constraints only forbid the Rigidbody from being rotated when the rotation happens a result of a force being applied.
So it will only stop a rotation from something like Rigidbody.AddTorque or from a collision.
Setting Rigidbody.rotation or calling Rigidbody.MoveRotation will always work regardless of freeze state, constraints or sleep because they mutate the rotation directly; they do not apply a force.
If you want to use Rigidbody.MoveRotation or Rigidbody.rotation or any Transform method, freezing the rotation of the rigidbody or adding constraints won't help. So instead, in MovementControls get mousedown the way you do in Straight and set the rotation only if it's false, like this:
if (!mousedown) { currentRotation.x += Input.GetAxis("Mouse X") sensitivity; currentRotation.y -= Input.GetAxis("Mouse Y") sensitivity; currentRotation.x = Mathf.Repeat(currentRotation.x, 360); currentRotation.y = Mathf.Clamp(currentRotation.y, -maxYAngle, maxYAngle); rb.rotation = Quaternion.Euler(currentRotation.y, currentRotation.x, 0); } Share My Card Statement
I used this from a tutorial, and replaced thee transforms with rigidbody, But I was told Atan2 was a bad solution. and I was wondering why that was.
if (moveInput != Vector2.zero)
{
float targetAngle = Mathf.Atan2(moveInput.x, moveInput.y) * Mathf.Rad2Deg + cameraMainTransform.eulerAngles.y;
rotate = Quaternion.Euler(0, targetAngle, 0);
playerRigidbody.rotation = Quaternion.Lerp(playerRigidbody.rotation, rotate, rotationSpeed * Time.fixedDeltaTime);
}
Your answer
Follow this Question
Related Questions
Player jitters when moving. 1 Answer
Script that stays behind player that rotates the player along with the camera/mouse (third person) 0 Answers
Camera Rotating when the player reach a specific position 0 Answers
Rotation of player based on camera direction and joystick direction in 3D world 1 Answer