2d Faux gravity

Hello, I am trying to make a game where you can walk around over smll planets, but I can’t get the gravity to work, I have foudn some videos online but none of them worked, so I was wondering if anyone could help me. I would like to be able to jump to other planets when you are closer to that planet, but that is just an extra so if that is not possible than that is no problem, the main thing is that I want to be able to walk around on a small planet. Thank you.

If you means something like gravitee wars(required flash player) youtube example, that’s kinda easy to implement but moderate difficulty to get it the way you want.

Gravity by itself is a quite simple thing and some parts can even be more simplified in a simulated environment. Generally I would recommend to create a single manager script which handles gravity. It’s possible to have each object that should be affected by gravity handle it on its own but for performance reasons it’s better to handle this at a central place.

The manager would keep a list of “gravity sources” and a list of “affected objects”. You may want to add a script to the affected object to communicate the current net gravity vector so the object knows where “down” is at the moment.

Now all you have to do each phyics frame is to iterate through all objects that are affected by gravity. Calculate the distance from an object to each gravity source. This distance vector is used to calculate the gravity this source is applying to our object. In reality the gravity depends on the distance squared. So the closer you get to the source the greater the pull is. So the force you want to apply need to be divided by the magnitude / sqrMagnitude of your distance. Though using the actual formula may not look / feel right at that small scale. You could instead use an AnimationCurve to define the gravity change based on the distance. I mainly refer to the example i’ve mentioned above.

When you have calculated the force / acceleration vector for each source just add the vectors up to get the resulting net gravity for that object. That means if you have two planets at a certain distance you get a zero gravity point between the two as the gravity of the two planets cancel each other out. Though since the gravity depends on the distance in most cases you get a non-zero gravity vector.

All you have to do is applying that net gravity vector to the object with AddForce. As mentioned above you may store that vector in a variable on a script on each object for later use. This is all you need to get the gravity working.

The controls to move are a different story. In the game i’ve linked the player actually sticks to the ground and they can’t jump. They basically have magnetic shoes so they can even walk on an overhang. So when they stick to the ground they aren’t affected by gravity. Though what you actually want is up to you. You have to define what behaviour you want. There is no right or wrong. It depends on the look and feel you want and how well one can play the game. Having physically correct behaviour but the player easily get stuck in edge cases or is trapped in uncontrollable situations is worthless. You should put your main focus on smooth gameplay.

As a final note: Making a game like the one i’ve linked above is quite hard. It has many different aspects and challanges that need to be solved. Some are easy some are harder. The more experience you have to easier things get. This is the moment your math teacher was talking about all the time. The moment where it’s important to actually understand a formula rather than just remembering it.