hello guys i need help adding gravity to this script and i want to no how to do it aswell if you can help me thanks
asked Sep 02 '10 at 09:49 AM
It looks like there is code in there to handle gravity. The problem is, it's adding a constant velocity to the y-movement of the object each frame. So it won't look right.
Real gravity is a force that produces acceleration- the velocity itself increases each second (until a maximum is reached due to wind resistance, or something else pushes back, like the ground.)
1) Easiest solution is to set the gravity variable to 0 (to turn it off.) Add a rigidbody to your object instead. By default its "use gravity" setting will be on. Just press play and watch it fall. This also means that it can hit other things like the ground, and Unity will handle it all for you.
Or, turn off that checkbox and instead of translating your object, you can apply forces. There is code here: http://answers.unity3d.com/questions/17341/changing-gravity-for-one-object Note that it's using the object's rigidbody mass and Unity's gravity constant, which are set by you in the editor, but you could plug in your own numbers there instead if you wanted.
2) If you can't use rigidbodies and forces for some reason, you'll have to add a new variable to track the current velocity of the object yourself.
Let's say we add a float gVelocity and set it to 0 in Start(). Then in Update(), change
(In this case I assume gVelocity is a float, to keep it simple. A more elaborate solution would be to store the velocity as a vector, then velocity wouldn't just be downwards.)
so is this right
sorry im just a beginner
answered Sep 03 '10 at 11:59 PM