I’m currently trying to make a custom gravity system where all masses are attracted to each other - at a decreasing rate, of course. I also want to make exceptions, where I can have masses that are affected by gravity but don’t generate it, or vice versa. Masses should be able to have different amounts of mass, as well as a customizable rate of gravity decay(the rate at which gravity weakens as something moves further away). How would I go about doing this?
You’d basically have to loop through each object in your scene. For each object you would find the distance to every other object and then apply a force to that object based on how far away it was and what mass it was. (You can use the universal law of gravitation Why do mass and distance affect gravity?)
You could consider the paid asset Circular Gravity Force that I believe handles everything you mention.
I’d rather not pay money for an asset, I’m just doing this for fun.
Also, I’d like to actually learn some programming, so I don’t think downloading an asset would help much
It’s not that complicated, newton actually figured out the math a while back for us
About the varying gravity, just move around G to get a different rate for each object.
you can modify the equation to divide by cubed(or anything you want) rather then squared to get a different falloff,
making stuff generate gravity but not take it is rather simple, what trouble did you encounter so for?
edit: put a link a hebrew version of this by mistake, hehe
The problem is that I don’t know how I’m supposed to script this in the first place. I’m pretty new to Unity and I’m not sure how a lot of stuff works, so I don’t know how I’m supposed to make the script in the first place.
What you’re trying to do is conceptually simple, and something I do all the time in Gravia. The biggest challenge will be avoid common performance bottlenecks.
In the simplest case, applying the gravitational force between objects is a simple matter. Gravity works such that two objects exert an equal but opposite force on each other based on their respective masses and the distance between them. The force is G * MassA * MassB / (Distance(PositionA, PositionB) ^ 2).
So, in a FixedUpdate method of a script attached to an object with a Rigidbody, you would do the following:
-
Find all other Rigidbodies in the scene
-
For each other rigidbody:
-
Compute the force between the two rigidbodies based on the formula above
-
Compute the direction vector between the two bodies.
-
Apply the force along the direction vector to the rigidbody
A couple of considerations:
As mentioned, the force should be applied to both objects. You could either apply that force to both objects in the script, or you could only apply it to the “other” object, and assume that the other object also has this script on it, and will apply the same force to “this” object later. The advantage of the latter approach is that you don’t need to worry about keeping track of a list of which objects have applied forces to which other objects this physics update. So, to keep things simple at first, I’d just have your script apply the force to the “other” object, but not to the object on which the script is located.
The other concern is that finding all rigidbodies in the scene is expensive to do all the time, especially if essentially all rigidbodies need to find all other rigidbodies every fixed update. It will work if you call FindObjectsOfType every fixed update, but the performance will be bad. I use a caching approach to avoid having to call FindObjectsOfType every frame.
Thanks dgoyette, this really helped!
To avoid the need to search every frame, make a static list of rigidbodys and every time one spawns make him register himself to the list