# Don't get Physics.gravity

New to coding and need a little help, I making a game in wich stuff falls from above and the player has to avoid them, the problem is that I don’t want the stuff falling at the same speed so I’m using this code to alter the gravity of the falling objects:

public class modGravity : MonoBehaviour {

``````public float grav;
public GameObject debreis;
void Start()
{
{
Physics.gravity = new Vector3(0, -grav, 0);

}

}
``````

}

How the game works is that there’s a block that’s a kinematic object, when the players enters a collider that’s parented to the block it stops being kinematic and starts falling.
The problem is that no matter what value I asing to the grav variable the objects all fall at the same “normal” gravity speed.
So is there something missing?

As you are using Vector3 above and didn’t specify I’ll assume this is 3D Physics.

I would instead provide an opposite force against the direction the object is falling. I recommend reviewing documentation on RigidBody.AddForce()

Depending on the force you add, and how often you add it, the object will appear to fall more slowly. Instead of modifying mass or gravity you are working against it in tiny increments.

Here’s some example code (which I commented for you) that I use to make a robot hover. This goes in FixedUpdate() not in Update(). By reducing the force which is added upwards below a certain threshold it will simply fall more slowly to the ground instead. I definitely think this is the way for you to go if working with 3d physics. You’ll have to play with the weights of things (which should be a standard across all falling objects) and then you can just adjust the force you are adding upwards on each object to determine how slowly it falls. Think of it like adding helium balloons of various sizes to the falling object since the net effect is the counteracting of gravity.

`````` //hover
Ray downRay = new Ray(transform.position, -Vector3.up); //prepare a ray to cast to the floor
if (Physics.Raycast(downRay, out hit))  //cast a ray to the floor
{
//Debug.Log("Floor hit distance: " + hit.distance);
float hoverError = 6 - hit.distance; //determine appropriate height based on a tolerance
if (hoverError > 0) //if the bot is not hovering high enough
{
//Debug.Log("Hover error is: " + hoverError);
CheckDistanceMove(distanceFromPlayer); //move toward the player
}
else if (player.transform.position.y > transform.position.y - 3) //if the player is elevated above the bot then move up regardless
{
//Debug.Log("pushing bot up to match player!");
rb.AddForce(11.0f * Vector3.up); //add a force to the bot to keep it in the air
CheckDistanceMove(distanceFromPlayer); //move toward the player
}
}
``````

In particular you will want to focus on this line:

`````` rb.AddForce(11.0f * Vector3.up); //add a force to the bot
``````

I highly recommend reading through ALL of the documentation in the Physics package or at a minimum browsing through all the methods in Physics so that you know what is out there. They are ALL highly useful! The world of 3D physics has broken many many a would be Unity C# developer… it broke me 3 times before I finally read ALL of the documentation before writing another line of code. It’s also only about 30 pages total.

Please let me know if you have any questions!