Hey guys i was just wondering how to change the constant force gravity on an object on collision
var hit = false;
function OnCollisionEnter(collider : Collision)
{
hit = true;
}
function Update()
{
if(hit)
{
rigidbody.AddForce(transform.forward * 10 * Time.deltaTime)
}
}
There are two ways to change the gravity affecting an object:
You can change the Physics.gravity constant to affect all objects or you can add gravity by hand
Editing Physics.gravity is simple (see the doc link) but affects EVERYTHING
To apply a diffrent gravity to this object, have a script that does the following:
- rigidbody.useGravity = false;
- Holds a
var localGrav : Vector3 = Vector3.up * -9.8;//down 9.8 m/(s*s) is earth's gravity
- Inside of LateUpdate,
[rigidbody.AddForce][3]( localGrav, [ForceMode][4].Acceleration );
Now just change localGrav and your rigidbody will move at a localized, custom gravity amount