How to make a real planetary gravity in Unity?

Want to create a game with a round planet with normal gravity, and objects to obey the laws of physics and that the player was always horizontal to the ground(as in Super Mario Galaxy) but don't know how.
June 27th 19 at 15:03
1 answer
June 27th 19 at 15:05
Solution
Hello. I, Cyril. Wish you made a game, 3D-ekshon the bottom line is this... the User can play wood elves, the Palace guard and a villain. And if the user plays with elves the elves in the forest, wooden houses nabuhaut soldiers Palace and villains. You can Rob korowai...


Do a lot of meshes (the planet in the form of a ball, player, etc.), add them about a script like this:
class Gravity : MonoBehaviou {
 static const double G = 9.8; // correct constant here

 static LinkedList<gravity> meshes = new LinkedList<gravity>();

 float Mass {
 get {
 return GetComponent<rigidbody>().mass;
}
}

 void Start() {
meshes.add(this);
}

 void Update() {
 foreach(var mesh in meshes) {
 float power = (float)(G*Mass*mesh.mass);
 var force = power*(mesh.transform.position - transform.position).normalized;
 force /= (mesh.transform.position - transform.position).sqrMagnitude;
GetComponent<rigidbody>().AddForce(force);
}
}
}</rigidbody></rigidbody></gravity></gravity>


Horizontal leave as homework. Tip - you need to rotate the mesh of the player so that it was collinear with the vector land-player.
Thank you !! And how to make sure that all objects are attracted only to the Planet(not to each other)? - raul.Koepp commented on June 27th 19 at 15:08
: Well, generally speaking, the issue is decided by the total difference in mass, then we can neglect the attraction of other bodies to each other ;) but in General you can make individual tags and to add to the list (branching at the start) or even two different script, but then have a little mock common sense, this option is the easiest and most "physically honest". - patrick.Bro commented on June 27th 19 at 15:11

Find more questions by tags C#Unity Game Engine