- Home /
Question by
Aladine · Jan 04, 2015 at 03:36 PM ·
2drigidbody2dracing
[Rigidbody2D] how to implement drifting in a top down racing game ?
Hello everyone, I am trying to make a top down racing game similar to VS.Racing2[youtube and am kinda stacked with the drifting feature, from what i understood by watching and playing that game i ended up with this code, however, the physics still doesn't feel right :
void FixedUpdate ()
{
//move car "forward"
rigidbody2D.AddForce (transform.right * moveForce * Input.GetAxis ("Vertical"));
//limit car velocity
rigidbody2D.velocity = Vector2.ClampMagnitude (rigidbody2D.velocity, maxSpeed);
//turning the car
if (Mathf.Abs (Input.GetAxis ("Horizontal")) > 0) {
//rotate body
rigidbody2D.angularVelocity = Input.GetAxis ("Horizontal") * -rotationSpeed;
//increase rotation force
if (rotationSpeed < 300) {
rotationSpeed += Time.deltaTime * 20;
}
//add opposite force (drift)
rigidbody2D.AddForce (-transform.up * (rigidbody2D.velocity.magnitude / 2) * driftStart * Input.GetAxis ("Horizontal"));
//decrease drifting multiplier
if (driftStart > 0.2f) {
driftStart -= 0.01f;
}
//decrease general speed
if (maxSpeed < 10) {
maxSpeed -= 0.5f;
}
} else {
//reset
driftStart = startDriftStart;
rotationSpeed = startRotation;
maxSpeed = startMxSpeed;
}
}
what i need to know is that either my approach is correct or not, so maybe am doing it right but i just to keep changing the values in order to have the best result, or, maybe there is a better way to do such a thing and am doing everything wrong since the start.
I would really appreciate your help. Thank you
Comment
Your answer
