- Home /
Mario Kart-style car Physics
I would like to make a racing game, like Mario Kart, so with very simple physics. Using car tutorials that are already out there, seem too complicated as I don't want all the features - no gears, rolling over if you're going too fast round a corner etc.
Ideally, all I want is a car that I can keep really simple. So having an object that accelerates up to a top speed, has a set turning angle and won't turn when its not moving and has simple rigidbody physics such as bouncing and skidding off walls and other carts. This is I think how the MarioKart carts work - when they get hit by a weapon they just have a simple animation - no physics.
What's the best way to implement this?
What about if you wanted to have an undulating track? Then you'd have to have a system where the cart tilted to align with the ground beneath it.
Answer by Cap · Jun 02, 2010 at 07:08 AM
I'll just post some code snippets that I'm using in my own project here along with some pointers for the rest. These don't take into account drag so you'll need to add some of that to your karts via their rigidbody, otherwise they'll never slow down once the player lets go of the forward key.
Acceleration and limiting top speed:
// for acceleration var throttleforce = accel; // this is the acceleration speed of your kart if(Input.GetAxis("Vertical")>0) { // forward key down rigidbody.AddForce(transform.forward * Time.deltaTime * throttleforce); }
// for limiting kart max speed
var v = rigidbody.velocity; // current speed
if(v.sqrMagnitude >= truemaxspeed){ // truemaxspeed is the max speed max speed. // calculate that in the awake or start events so you don't bog things down. rigidbody.velocity = v.normalized maxspeed; }
You should be able to use the steering from existing car tutorials and just cut out the bits you don't need, so I won't bother posting that code here. Remember you can use Time.deltaTime as a multiplier for your kart spec values to make them time based, thus letting you set a particular degrees per second rotation for your limited turning circle.
The physics can be handled quite easily with rigidbodies and you can lock down rotation along x and z and movement along y (if you don't need jumps on the course) using a configurable joint or by just forcing those values to 0 in code.
Setting up what happens when a kart is hit by a weapon can be handled by checking the IsTrigger box on the weapon's collider and then using the subsequent event to spin the kart or whatever you want to have happen.
What exactly do you mean by a 'configurable joint?' Does that mean you could align the car with the ground beneath it, as that would be useful for sloping tracks.