- Home /
Planetary physics for player
I'm working on a planetary shooter where the planets looks something like this:
I've figured out how to get non-static objects to fall to the ground:
var planet : GameObject;
var gravityObject : GameObject;
var acceleration : float = 2.5;
var isGrounded : boolean = false;
function Start ()
{
}
function Update ()
{
gravityObject.rigidbody.AddForce((planet.transform.position - gravityObject.transform.position).normalized * acceleration);
}
But when I apply this to my character (and mobs), I want their bottom to always face the middle of the planet, like in real life, when you for example walk up a hill, you stand straight up relative to the middle of the earth, instead of leaning backwards so that you are standing straight up relative to the ground. Can you explain how I can possibly code that (without pre-made assets)?
I know there is much questions like this, but no one has really answered MY question.
Thanks on beforehand!
Answer by robertbu · Sep 03, 2014 at 04:57 AM
I'm not where you are having trouble with the many other answers to this question. The first task is to get a vector that defines up. From your drawing, you can do it in one of two ways. You can treat the planet as a sphere and use (object.position - planet.position). The second way is to use Collider.Raycast() and use the 'hit.normal' it returns. Assuming the first, a script on the object to be aligned would need to execute the following each frame:
var up : Vector3 = transform.position - planet.position;
transform.rotation = Quaternion.FromToRotation(transform.up, up) * transform.rotation;
Thanks! Couldn't vote up, because I didn't have enough reputation ;)
Your answer
Follow this Question
Related Questions
How to simulate moon gravity? 3 Answers
Gravity and rotation control 0 Answers
Planet gravity help need guidance no code 1 Answer