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
17 Likes

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.

1 Like

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.

1 Like

Just came here to ask about this problem, thank you!

1 Like

I’m familiar with Java and a complete c# noob, but I’m interested in how that sets the constraints, property/variable? I should probably not ask this until I learn more about c#, but I understand the bitwise or with compare what I think is ints from those three static variables (sorry if this Java terminology is incorrectly crossed over here) and ends up with a int which represents a value for x,y,z, is it a bit like octal permissions is Linux?

Sorry I’m rambling and probably wrote something completely unintelligible. ::

You’re not far off. RigidbodyConstraints is an enum type and the values are the decimal equivalents of bit values, so 2, 4, 8, 16… (although there are some that combine several bit values into one constant).
FreezeRotationX is 16, FreezeRotationY is 32, and FreezePositionZ is 8.
and there is a combined value for FreezePosition of 14. (2 +4 +8 for the corresponding X, Y, Z constants).

    private Rigidbody rb;
    private RigidbodyConstraints cachedConstraints;
    void Awake()
    {
        rb = GetComponent<Rigidbody>();
        cachedConstraints = rb.constraints; // Cache constraints set in nspector
    }
    private void ResetConstraints()
    {
        rb.constraints = cachedConstraints; // Reset constraints to cachedConstraints value
    }

You don’t want to hardcode inspector valeus in script, or depend on Enum bit order.

1 Like

Yes, I wasn thinking about something similar, although I was more naively just doing

var originalConstraints = rb.constraints;

rb.freezeRotation = true;
transform.Rotate(rotation * Vector3.forward);
rb.constraints = originalConstraints;

I guess the benefit here is that we don’t have to worry if those constraints get changed by anyone else out of our control. If those constraints are a simple bitfield value, I would think it’s okay to just fetch it here locally instead of caching a value?

Yes. It just depends on what you want to do at that moment. Do you just want to unhinged the lock for a moment, do a rotation and put the lock back on again, or is there some sort of preset you want to snap back to…

1 Like

Privacy & Terms