- Home /
How do I speed up the simulation of a rigidbody?
I really doubt this is possible, but I'm just asking for confirmation.
In a scene where several rigidbodies are being simulated, is it possible to slow down the simulation speed of just one of them?
I may just want to change the gravity and add drag.. any thoughts on what I could do?
Answer by aldonaletto · Feb 03, 2012 at 11:10 PM
You can turn Use Gravity off and set the mass to a huge value - this will slow down reactions to collisions or applied forces. The gravity may be simulated with a few lines in FixedUpdate:
var myGravity = 4.8; // set the local gravity (regular gravity is 9.8)
function FixedUpdate(){ var curVel = rigidbody.velocity; curVel.y -= myGravity * Time.deltaTime; // apply fake gravity rigidbody.velocity = curVel; } NOTE: This is physically correct - the rigidbody will suffer an acceleration similar to gravity, but with a different value. Like in real life, the acceleration doesn't depend on the object mass.
Thanks! I'll have to experiment with that. It isn't a really specific formula though, so I'm assu$$anonymous$$g it will take some tuning to get it right.
I'm sorry I didn't word my question the best. What I meant was speeding up or slowing down the simulation. It would be an adjustable parameter that would dynamically change throughout the game.
It's actually a car that's being simulated, so the tire friction model doesn't behave with changing the mass. I tried altering the fixed time step in the time manager, and that's when I figured out that that's gong to be a problem. If the car is in slow motion, it's going to slide around too much. If it's in fast motion, it will topple over because the friction model got too grippy.
Ouch! That's a huge problem: the wheel collider uses a dynamic friction model independent of the physics engine.
Is this happening when you change Time.timeScale? If so, try to modify the forwardFriction.stiffness and sidewaysFriction.stiffness of each wheel collider according to the timeScale change:
var stiff = 1.0 / Time.timeScale;
wheel.forwardFriction.stiffness = stiff;
wheel.sidewaysFriction.stiffness = stiff;
This will increase the grip in slow motion and reduce it in fast motion. If the behavior is still incorrect, try a quadratic function (use stiffness = stiff * stiff).
Hey, that timeScale worked awesome!! That's a really good start. The problem though is that the server has to simulate bot cars. That means that if your player slows down, everyone else in your world who is getting simulated has to slow down too.
The dead reckoning went a little crazy too, but that's nothing I cant fix with a little multiplication.