- Home /
Replicate physics.gravity with custom code.
I'm working on a kart game, and I want to be able to play with the gravity. I have a simple box with cylinders fitted with wheel colliders. I have turned off the 'useGravity' and tried various methods to apply custom gravity, such as rigidbody.AddForce, as well as a ConstantForce component. First I set them all to the same value as the default physics.gravity, then I tried other numbers. When using this custom gravity the kart does not respond well at all: bumping, sliding, turning in odd directions.
Here is one example the relevant code:
function Update(){
ApplyGravity();
}
function ApplyGravity(){
rigidbody.AddForce(-Vector3.up*Physics.gravity.magnitude);
}
Hmm. I just read on the Physics $$anonymous$$anager document that the default gravity setting is meters per second. I need to look up a way to translate this to AddForce.
Answer by sparkzbarca · Nov 19, 2012 at 06:47 AM
gravity is per second. It should be applied during fixedupdate.
Your applying 9.8 meters of force every whatever. Whatever is an unknown. update runs as many times as it possibly can during the game. Slow comps run less often for example.
FixedUpdate runs BY DEFAULT 50 times per second.
if you want 9.8 meters per second then in fixed update run it but applyGravity should be
(-vector3.up * 9.8 ) / 50
Roughly
just in case thats not clear.
your problem is that if your running the regular update more than once per second your not portraying gravity because that much force should only be applied over a full second.
if your running that 50 times a second your replicating 50 times the force of gravity. and since update varies in speed your actually changing gravity. Its not a constant since update varies in frequency.
If I multiply the 50 rather than dividing, it works perfectly now, thanks!!
rigidbody.AddForce(-Vector3.up*9.8*50);
rigidbody.AddForce(-Vector3.up*9.8*Time.deltaTime);
deltaTime is the time between 2 frame (~1/50 but is more accurate)
if you'll notice i assumed he would run it in fixed update, in which case fixed update is 50 frames per second by design. Unless your game goes below 50 frames
@sparkzbarca: not entirely correct. Yes, if nothing gets ever changed in the settings, Time.deltaTime will default to 1/50 in FixedUpdate(). But actually it depends on the value of Edit->ProjectSettings->Time->FixedTimestep (which is Time.fixedDeltaTime). You should never use hardcoded numbers in scripts that could refer to modifiable values.
On a sidenote, if you scale your simulation values with Time.deltaTime, the ti$$anonymous$$g will be "correct", whether you do your calculations in Update() or in FixedUpdate(). The difference is that doing it in FixedUpdate() guarantees that it is executed regularly at equdistant time intervals, while when using Update() it is executed each frame, and the time interval depends on your framerate.
Answer by XienDev · Nov 19, 2012 at 07:22 AM
Add constantForce to your gameobject, and play with it =)?