rb.freezeRotation - need more explanation - Project Boost video 39

In the Complete C# Unity Game Developer 3D course, @7:25 of video 39 in the project boost section, we wrote the code below to disable the physics engine of the rigidbody when the player is rotating the object. This was breezed over in the lecture and I would appreciate a better explanation. I understand the concept, but I’m not sure how it’s working. I under stand that the rb.freezeRotation = true/false, freezes and unfreezes the rotation of the rigidBody, but what conditions are the true/false referencing?

I would have imaged that we would have created and if/else if statement that froze the physics of the rigidBody when colliding with a box collider and unfroze the physics when the rigidBody is not colliding with anything. For example, if rigidBody is colliding with another object than rb.freezeRotation = true, else if rb.freezeRotation = false.

Freeze physics code from lecture:

The condition is that we have pressed the left or right buttons and are applying rotation manually and don’t want the physics engine to do that.

No, we want the physics to handle rotation when there are collisions. The only time we don’t want it to handle rotation is when we are handling rotation ourselves.

We are telling the physics engine to not rotate the rigidbody. A rigidbody is usually controlled by physics and the physics engine calculates all those rotations and movements. What we’re doing when we set the freeze to true is to tell the physics engine that the rotation should not be calculated. Under normal circumstances we want the physics engine to control the rigidbody

I understand what it’s doing and why it’s doing it. I guess I just don’t understand the flow of the code. How does it know which statement to execute when they are both encapsulated within the same method without any further direction? There’s nothing within that method that’s referencing when we are pressing vs not pressing rotate. Sorry, I’m new to coding and trying to get better. To me is seems like when the ApplyRotation(); method is executed the rb.freezeRotation = true; and rb.freezeRotation = false; would both trigger and cancel each other out.

Oh, I see. We are passing in the rotationThisFrame. That is either positive or negative and that affects the direction we rotate in. You will see that when we call the function we pass either rotationThrust or -rotationThrust depending on which button was pressed

Thanks for all your help . This is the answer I was looking for. I didn’t realize that the statements run sequentially. I thought It was processed all at the same time within the ApplyRotation() method.

The rb.freezeRotation = true and rb.freezeRotation = false lines do not cancel each other out because they are executed sequentially within a single frame:

  1. Freeze rotation.
  2. Manually rotate the GameObject.
  3. Unfreeze rotation.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms