- Home /
How to make car land on 4 wheels and maintain speed?
So I have made a simple physics for my car game. It is a top-down racer, similar to NES era games, here's a screenshot for you to get the general idea - LINK
You can drive the car forward/backwards with standard acceleration, but driving to the sides is like strafing, the car doesn't turn to the left/right and it doesnt rotate in these axes. The only axis which he can rotate is X, that means it can rotate forward/backward (like when driving uphill/downhill). I didn't want the car to rotate like crazy (too easily, too fast), so I wrote a script that stops the rotation to some extent:
if (transform.eulerAngles.x >= 30 && transform.eulerAngles.x < 90 && !IsGrounded()){
rigidbody.AddTorque (-7*Time.deltaTime, 0, 0);
}
if (transform.eulerAngles.x > 270 && transform.eulerAngles.x < 330 && !IsGrounded()){
rigidbody.AddTorque (7*Time.deltaTime, 0, 0);
}
Visualization if you don't understand what happens - number 2 and 4 - CLICK
It works fine, except for when the car hits something with high speed when it is rotated (number 3 and 5 on picture above) - the force that gets applied to the rigidbody during collision is so high that my script doesn't stop it (because it adds just a moderate amount of force - otherwise it makes things even worse). Yet another picture: CLICK
Even though I blocked the car entity from rotating in 2 axes, I can't do it in third, because then it would be impossible to drive uphill/downhill
I wanted to know if there is a way to make the car land "gently". I don't want to use preset rotation, resetting Quaternion or force values, because that makes the car "jump" to that setting in 1 frame. If I add yet another limiter similar to the script above, it is band-aid solution, because if force is strong enough it will overcome the script. I tried adding manual rotation control mid-flight, but it doesn't stop the player from rotating the car into the "flipping" angle. If I limit the rotation by a script (sorry for pseudo-code, but I can't find it):
if(rotation > 60){
rotation = 60;
}
It often makes the car stuck in this angle. I thought a solution might be something like this - CLICK
but I have no idea how to implement box colliders with constrained global rotation attached to the car, and I don't even know if that would work
I just came up with that script, which seems to solve the problem in a lot of cases, but still if the force is too big it seems to either fail or make car lose forward velocity entirely (which I want to keep - car drives at speed 20, falls down, lands with angle 55 degrees rotated forward, continues at speed 20 or slightly less - a picture of it - CLICK ):
if (transform.eulerAngles.x >= 30 && transform.eulerAngles.x < 90 && !IsGrounded()){
if(transform.eulerAngles.x >= 60 && transform.eulerAngles.x < 90){
transform.eulerAngles = Vector3(59.0, 0.0, 0.0);
rigidbody.angularVelocity = Vector3.zero;
rigidbody.AddTorque (-10*Time.deltaTime, 0, 0);
Debug.Log("301");
}
rigidbody.AddTorque (-7*Time.deltaTime, 0, 0);
}
if (transform.eulerAngles.x > 270 && transform.eulerAngles.x < 330 && !IsGrounded()){
if(transform.eulerAngles.x <= 300 && transform.eulerAngles.x > 270){
transform.eulerAngles = Vector3(301.0, 0.0, 0.0);
rigidbody.angularVelocity = Vector3.zero;
rigidbody.AddTorque (-10*Time.deltaTime, 0, 0);
Debug.Log("301");
}
rigidbody.AddTorque (7*Time.deltaTime, 0, 0);
}
I am open to suggestions, I've started with unity 4 days ago and I still don't know some basic stuff, forgive me if it has been answered (probably didn't know the right keywords). Thanks for reading :)
Your answer
Follow this Question
Related Questions
Tracking Rotation for Flips/rolls 3 Answers
Alternative to Quaternion.LookRotation() ? 2 Answers
EulerAngles.x stops at 90??? 2 Answers
How to get rigidbody local z axis rotation in Euler angles? 0 Answers