I do not understand the bug honestly

Hey everyone. I tried to see the video slowly and I still do not understand what expectation was not met there. Can someone explain more?

Hi shampoo,

Welcome to our community! :slight_smile:

What bug do you mean?

The bug discussed at 5:35. I don’t understand what’s wrong.

Please explain the bug as if I can’t watch the video. Not everyone on these forums have all the courses.

There is a rocket and we control the rotation of the rocket and the thrust. The mission is to take the rocket from point A and land it to point B. We add an obstacle and when the rocket hits the obstacle, the rocket will act funny when using thrust again.

Why is it funny and this is a bug, I don’t know. That’s my question.

The workaround appears to be freezing rotation on processing thrust (in the script), and re-enabling the rotation when the thrust is done. This is the method (ignore the audio part):

        if (Input.GetKey(KeyCode.Space))
        {
            rigidbody.freezeRotation = true;
            rigidbody.AddRelativeForce(Vector3.up * Time.deltaTime * thrust);
            rigidbody.constraints =
            RigidbodyConstraints.FreezeRotationX | // freezing rotation on the X
            RigidbodyConstraints.FreezeRotationY | // freezing rotation on the Y
            RigidbodyConstraints.FreezePositionZ; // freezing position on the Z   
            if(!audioSource.isPlaying){
                audioSource.Play();
            }
        }
        else{
            audioSource.Stop();
        }

Why do we need to do this?

We are manually changing the rotation of the rocket. This does not affect the rigidbody so according to the rigidbody’s internal data no rotation has been applied. As soon as we hit something, the physics system applies rotation to the object. Now when we apply relative force, the rigidbody takes this new rotation into account - which it didn’t do before because there were none - and the rocket does not behave in the predictable way we programmed it to do.

If you open the ‘info’ panel on the rigidbody you can see what I mean (look at angular velocity)
image

When we don’t hit anything, the angular velocity remains unchanged even when we rotate. The moment we hit something, angular velocity gets applied and this is where stuff goes out of whack.

As we’ve seen, manually rotating the object does not affect the rigidbody but hitting something does. When we manually rotate the objects and we’re hitting something, we are sort of rotating the rigidbody ‘out of sync’. The collision created angular velocity that does not match the actual rotation of the object because we also rotated the object.

What we are doing when we are freezing the rotation is to try and make sure that we are only doing one of these things at a time - only use the rigidbody’s rotation when we are not manually rotating, and only use the manual rotation when we are pressing the buttons (if that makes sense). We don’t want these two things to happen at the same time.

2 Likes

Thanks. That makes a lot of sense and very clarifying.

I wonder if manually changing the rotation would make the best physics experience. There are probably better ways to do this. So when the rocket starts spinning, fix the rotation with force not manually changing it.

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

Privacy & Terms