- Home /
Mario Kart Style Drifting
Hey guys,
I'm working on a Mario Kart style racing game, and I'm doing the kart controller script. Basically what I have here is really rough but this is what I have so far, I'm going for rigidbody based movement.
What I'm looking for is a little advice on doing this, as I've never programmed a vehicle before and it doesn't 'feel' great, it works, but something just feels wrong (it seems to 'stick' to accelerating for a second before reversing, and vice versa). Also the drifting code is super basic but I'd like to get something very similar to Mario Kart's drifting system.. as I'm a bit new to applying rigidbody forces like this, I just wanted to hear your thoughts on it.
Here's my code:
void FixedUpdate()
{
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
isDrifting = Input.GetKey(KeyCode.Space);
if (inputY > 0)
{
// If we are going forward
if (currentSpeed < maxSpeed)
currentSpeed += acceleration;
}
else if (inputY < 0)
{
// If we are going backward
if (currentSpeed > -maxReverseSpeed)
currentSpeed -= decceleration;
}
else
{
// If we are stopping
if (currentSpeed > 0)
{
currentSpeed -= decceleration;
}
else if (currentSpeed < 0)
{
currentSpeed += acceleration;
}
}
if (!isDrifting)
{
// Set the drift direction while not drifting (we can't change direction mid-drift!)
if (inputX > 0)
driftingDirection = DriftDirection.Right;
else if (inputX < 0)
driftingDirection = DriftDirection.Left;
}
// Apply currentspeed as rigidbody velocity
this.rigidbody.velocity += transform.forward * currentSpeed;
// Handle kart drifting
if (isDrifting)
{
if (driftingDirection == DriftDirection.Right)
{
// Drifting right, add some velocity from the left
this.rigidbody.velocity += -transform.right * driftingSpeed;
this.rigidbody.AddTorque(transform.up * 1 * steeringAngle * driftingAngle, ForceMode.Acceleration);
}
else if (driftingDirection == DriftDirection.Left)
{
// Drifting left, add some velocity from the right
this.rigidbody.velocity += transform.right * driftingSpeed;
this.rigidbody.AddTorque(transform.up * -1 * steeringAngle * driftingAngle, ForceMode.Acceleration);
}
}
if (!isDrifting)
{
// Turn normally if not drifting
this.rigidbody.AddTorque(transform.up * inputX * steeringAngle, ForceMode.Acceleration);
}
}
I'd really appreciate any advice about how to handle these sort of physics. This is on a rigidbody that is not using gravity by the way, I will be handling my own 'stick to rotation of the road' code to stop the kart from being able to turn upside-down.
Thanks, -Ben
You should take a look at this for moving the "character". http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html
Your answer
Follow this Question
Related Questions
Car exhaust Flame in unity 1 Answer
How is wheelCollider RPM calculated exactly? 1 Answer
How to make car accelerate/decelerate over time 2 Answers
Realistically drivable car 0 Answers