Use this code instead

I would suggest using this code instead, as it will not free all of the rotation constraints, after rotating, which rb.freezeRotation = false would do. This new code will only unfreeze the rotation on X and Y, and make sure that the position on the z-axis will be frozen

        rb.freezeRotation = true; // freezing rotation so we can manually rotate
        transform.Rotate(Vector3.forward * (rotationThisFrame * Time.deltaTime));
        rb.constraints =
            RigidbodyConstraints.FreezeRotationX | // freezing rotation on the X
            RigidbodyConstraints.FreezeRotationY | // freezing rotation on the Y
            RigidbodyConstraints.FreezePositionZ; // freezing position on the Z

Thank you for this code :slight_smile:

1 Like

And of course, if this were a more complex game we should have some place to define the wanted constraints accordingly instead of hard-coding them inside the script back to what we defined in the Inspector originally… :smile:

1 Like

Yes I suppose, I might have a look at that, thanks.

Thanks. I should have guessed this was a possibility. Thanks for the code.

Thanks for the code, I had to add ‚ÄúRigidbodyConstraints.FreezePositionZ;‚ÄĚ at the end because my rocket was hitting obstacles and going off course on the z axis.