A solution for avoiding resetting the constraints

Most people, to fix the problem, already mentioned using AddTorque() instead Rotate(), but in my opinion Rotate gives a better control and overall feeling, yes there is a bit of stuttering during the rotation but it is negligible.

The primary problem most people have in this lecture using FreezeRotation, is that all other constraints reverts back to false after the method is called.

I found a workaraound using this code, in replace of the “FreezeRotation” one:

Basically this code just flags every constraints that you need for the bug to stop and instantly backs up to the initial state.

Since all the code block is basically called at the same time you won’t notice the tick in the inspector, but you can feel it working by testing it.

If you feel extra fancy or OCD is tingling, you can put the second rigidBody.constraints in the else block of the Rotation method, so it will behave properly in the inspector, heres the example:

Hope i helped someone.

1 Like

Thanks, but this is not a complete solution because it doesn’t unfreeze the Z rotation when it’s done.

weird, on my project works, can you share your script?

My code was literally a copy of yours, and I don’t see any part of your code that calls for an unfreezing of the Z rotation. If there’s a point where it does, please explain.

I probably need to mention my constraints setup on the inspector, maybe if the checkmarks are different can cause issues:


in the else node i removed the FreezeRotationZ from the lines where the logical operators are:

The code in the else node fires up when you are not rotating and it check for the constraints that are listed in there, toggling off everything that is not listed in the logical operators, so even the RotationZ that is not in there but also x and y position.

try checking in the inspector at runtime if you see any weird behavior with the checkmarks in the constraints section.

i also include my code for easyer copy paste and checks if everything is fine

void Rotation()
        if (Input.GetKey(KeyCode.A))
        else if (Input.GetKey(KeyCode.D))
            rigidBody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | 

    void ApplyRotation(float rotation)
        rigidBody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | 
                                RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezePositionZ;
        transform.Rotate(0, 0, rotation * Time.deltaTime);    

Hope we figured it out your problem.

P.S. there is some extra code implementation from further in the course, you can delete it.

Privacy & Terms