Physics issue

I’ve run into an interesting issue with physics using rigidBody.freezeRotation. Using it as shown in the lecture, the ship tips over very, very slowly when no buttons are being pressed. I experimented and discovered that setting it to true only when a A or D is being pressed, it behaves naturally again. Is anyone else experiencing this?

With the code as it is in the lecture:
ShipTip1

“Fixed” functionality:
ShipTip2

 private void Rotate()
{
    float rotationThisFrame = rcsThrust * Time.deltaTime;

    // Temp hack for testing. WHY DO I NEED TO DO THIS???
    if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
    {
        rigidBody.freezeRotation = true; // take manual control of rotation
    }

    if (Input.GetKey(KeyCode.A))
    {
        transform.Rotate(Vector3.forward * rotationThisFrame);
    }
    else if (Input.GetKey(KeyCode.D))
    {
        transform.Rotate(Vector3.back * rotationThisFrame);
    }

    rigidBody.freezeRotation = false; // resume physics control of rotation
}
2 Likes

I too would like to know this!
The ‘hack’ works as described.

Could we get an update on this?

Edit:
I just tested the code provided above: It introduces a new bug where the Z position can change, thus the ship coming closer or further away from the camera.

Have you checked the Constraints to freeze position on Z?

Interesting in my code I tried it and I had completely different results. It made my ship very unstable.

Many factors involved I guess.

I had the exact same issue, and also fixed it by freezing the rotation only when pressing A or D .

Privacy & Terms