Answer the question
In order to leave comments, you need to log in
How to make real planetary gravity in Unity?
I want to create a game with a round planet and with normal gravity, and so that objects obey the laws of physics and that the player is always horizontal to the ground (as in Super Mario Galaxy), but I don’t know how.
Answer the question
In order to leave comments, you need to log in
Hello. I am Kirill. I would like you to make a game, 3D action is the essence of this... User can play wood elves, palace guard and villain. And if the user plays as elves, then elves in the forest, wooden houses are crowded with soldiers of the palace and villains. You can rob cows...
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);
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question