I’m making a game with potentially hundreds of cars on screen at one time… and I believe unity’s build-in wheel colliders do suspension calculations that are unnecessary to my game.
Is there a simpler(more efficient) method of making an object “slide and steer” along the terrain to simulate driving? I tried putting a “slippery” physics material on cubes and limit the friction direction and it kinda worked but was quite sketchy and didn’t seem right, maybe there’s a specific way of using “limited friction” that I don’t know about?
I had the same problem using wheelcolliders in a racing game. So, I wrote one myself. (C#)
/// <summary>
/// Use in FixedUpdate
/// Define maxForwardSpeed, grip somewhere ( I had mFS = 30f, g = 1.5f)
/// </summary>
/// <param name="inputForward"></param> range between -1..1
/// <param name="inputSideways"></param> range between -1..1
public void movement(float inputForward, float inputSideways) {
//Normalize Inputs
if (inputSideways < -1)
inputSideways = -1;
if (inputSideways > 1)
inputSideways = 1;
if (inputForward < -1)
inputForward = -1;
if (inputForward > 1)
inputForward = 1;
if (rigidbody.velocity.sqrMagnitude < maxForwardSpeed * maxForwardSpeed && isGrounded()) {
//forwardSpeed
rigidbody.AddRelativeForce(Vector3.forward * forwardSpeed * inputForward, ForceMode.Acceleration);
//Angular damping
if (inputSideways == 0) {
rigidbody.angularVelocity *= 0.5f;
//rigidbody.velocity = Vector3.
}
//Anti-Roll
rigidbody.AddRelativeForce(Vector3.down * antiRoll);
//Anti-Slide/Drift
rigidbody.velocity = Vector3.Lerp(rigidbody.velocity, rigidbody.rotation * Vector3.forward * rigidbody.velocity.magnitude, grip * Time.deltaTime);
}
}
bool isGrounded() {
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, LayerMask.NameToLayer("Ground"))) {
if(hit.distance - transform.localScale.y /2 < 1f) {
//Debug.Log("Grounded");
return true;
} else {
return false; ;
}
} else {
return false;
}
}
Add this code in a class which derives from Monobehaviour and add is as a new component to your car ( must have rigidbody attached).
A big plus is, that you can use the same car physics for both AI and Human Drivers. Just simulate the input Vector2 ([-1…1],[-1…1])