The car turned upside down

Hi recently I was making a car that runs with wheel colliders! Now I’m trying to make a script that will turn the car to normal position when it overturns… I try many ways and nothing… Any suggestions?

Set its transform.rotation property back to whatever you want it to be… try Quaternion.identity first.

Unfortunately it doesn’t work. Maybe i am doing something wrong…
here is my code:

        if(transform.rotation.z > 60 || transform.rotation.z < -60)
        {
            transform.rotation = Quaternion.identity;
        }
        if (transform.rotation.x > 60 || transform.rotation.x < -60)
        {
            transform.rotation = Quaternion.identity;
        }

Are you running the code in the Update method?

Yes I am, and I call this method at Start.

@CheeseFriz

First you should define how you know if car is flipped. Unity rotations are stored as Quaternions (which I know little about) so using those euler values from rotation isn’t the best way.

You could calculate the angle between World up vector and car up direction for example. If angle between your car transform.up and world up is more than 90 degrees car must be at least on its side.

When you know your car is flipped, you can either instantly flip the car by setting the rotation or if you want to rotate smoothly to your correct rotation, then you could use a coroutine. In this case you also need to define your target rotation. Quaternion.identity won’t usually be enough, if your vehicle has a set heading in 3D world you don’t want to change that.

1 Like