I have the following implented for a simple cart racing game… so far everything works well except I am wondering (a) how to implement a simple friction force to reduce the velocity over time when the player stops accelerating and (b) how to implement a smooth acceleration force from the user input (currently reaches top speed almost immediately).
Originally attempted using wheel colliders but couldn’t get it to work in a reasonable way (cart would be driven into the ground). Most of the wheel collider tutorials seem to be focused on realistic driving physics (not what I am after)
Have tried a lot of options and haven’t been having much luck with any of them…
if it was driving into the ground, i’m imagining the problem is you attached the wheels to the wheel meshes, which is wrong, because wheel meshes rotate, and wheel colliders expect to stay pointing straight down… You need to put them onto a separate gameobject so they dont rotate. What I do is create the wheelcolliders on the meshes, then re-parent them.
As for realism, its just a case of tweaking the wheelcolliders. Ive made cars than handle like go carts.
I had them on an empty game object - the problem was trying to apply a torque that would make the cart move fast enough, it seemed to overbalance the cart straight away instead of moving.
Do you have any examples or tutorials you could possibly point me towards?
Odd that it would disappear into the ground then. I had the problem at first but it was because I was rotating the colliders incorrectly.
The flipping problem is generally caused by the rigidbody center of mass being too high. You can adjust this via rigidbody.centerOfMass. Setting the Y of the vector to 0 will usually give you a good result. I had the same problem with flipping cars. My cars can turn on a dime at high speed now without flipping.
Have looked at the car tutorial but not sure it is going to work in this instance… some of my wheel models aren’t circular and the movement of the car must be very erratic.
Back to my original question - can anyone point me in the direction of applying a drag force to my velocity?
To reach a desired velocity, add a force of (desiredVelocity - currentVelocity) during FixedUpdate. Clamp that vector by your Acceleration variable. When decelerating, you’ll want to clamp it by a Deceleration variable instead. For AddForce, you’ll probably want to use ForceMode.Acceleration, I think.
This is very basic, but understanding this simple formula will help you think of new ways to apply forces as you desire.
If all else fails, just use simple conditional logic for the smooth acceleration:
if (speed > desiredSpeed) {
speed += 0.1;
}
if (speed >= desiredSpeed speed < anotherSpeed) {
speed += 0.05;
}
this should give you a steady transition from different intervals of speed