Hi there!So basically I’m messing with the physics of a little car game that I have made and right now I want to know how I could make it so when my car goes upside down so my Z is greater than 70 or something like that the game restarts.I have tried using a bunch of stuff to be honest and watched things on youtube and couldn’t figure it out.I can’t set something related to the Z rotation as I get some sort of error and i think that is because the game is 2D.Can you guys help me with some coding suggestions for this little thing?I’ve tried using a trigger and then oncollisionenter2D but that didn’t work neither even though it was on top of the car so when you were upside down it was supposed to trigger and tried detecting when the Z rotation is greater than 70 but no luck.I’m waiting for your suggestions friends ![]()
First, on the transform there is a bunch of helper vectors.
One of the vectors is called .up. It is different from Vector3.up, which is a constant, always (0,1,0)
The transform.up for any given transform is the local notion of +Y for that transform, however it is tumbled around.
You can query it and see if the Y component is negative, or even “very negative”, and use that to decide “hey, I’m upside down.”
Something like this:
if (transform.up.y < -0.5f)
{
// your game has ended, you are fairly inverted
}
I use this for detecting when you contact the ground inverted in my Jetpack Kurt Space Flight game. In my OnCollisionEnter() function, I do exactly the above check, and consider any type of inverted impact to be death.
For some more math behind it: the value of this vector is the mathematical cosine of the number of degrees you are from vertical. In other words, if zero degrees is vertical, cosine of zero degrees is 1.0. If you are at 45 degrees, the cosine of 45 is 0.707 and so on. Cosine of 90 is zero (your car is lying precisely on its side), and cosine of 100 (slightly beyond lying on your side) is -0.17. You can use this to plan how far an angle you want to allow your car to flip over, expressing it as this cosine value. Completely inverted would be -1.0 but it would almost be impossible to land up precisely inverted, so don’t use that value.
Caution: the Mathf.Cos() function accepts radians, not degrees. You can use Mathf.Deg2Rad to convert.
A few notes to Kurt’s very helpful comment:
The y component of the up vector is indeed the cosine of the degrees from the vertical, provided that Up is normalized (i.e. it’s length is 1.0). Unity’s Transform will normalize up, so this works well, but you should keep that in mind if you are using your own vectors that do not derive from Up.
Also, I try to avoid invoking Deg2Rad, since there is a linear relationship between rads and degrees, and a simple multiplication (or division) can do the same. Since
360 deg ~ 2 * Pi rad
1 degree ~ 2 * Pi / 360 rad = pi / 180 rad = 0.01745 rad
So I simply define a const degrees = 0.01745 (or more digits if you need the precision)
and I can simply multiply my angles (in degrees) like so
Mathf.Sin(myAngle * degrees)
And to convert rads to degrees, simply divide by degrees, or define a const rad = 1/degrees
It’s a small thing, I know - but you’ll be happy to know this when you are writing your first shader and need every last bit of performance, believe me ![]()
These are both extremely helpful!
Thank you so much guys, now things make sense a bit more haha, as I’m pretty new to Unity and C#.
Awesome explanations.Thanks again! ![]()
Another alternative would be to periodically send a raycast out the top of the car. If the raycast hits the ground you know you’re upside down.
Honestly, I don’t know how to do that haha
I am new to Unity and C# as a whole so I’m not really familiar with a lot of things.
But I used the other method that was provided and it worked really well.