- Home /
3d gravity on a planet
I have this really annoying problem which I cannot seem to get to work, I am trying to make a planet, and have it so that my rigidbody character always has downwards gravity (already done) and is rotatating so that he always has the bottom side down (this seems to be the had part), any ideas? Thanks!
Sorry, I need to clarify a little bit (this might make it a little more complicated) $$anonymous$$y planets are cubes, and I have the gravity working so if you are on one face then the gravity is down on that face etc., I just cant figure out how to get the rotation to work
Answer by syclamoth · Sep 14, 2011 at 11:19 AM
Well, if your planets are cubes then it's much easier to fake it. Just get the normal of the face you are currently on, and apply gravity in that direction! As for the rotation issue, try defining the axis of rotation and doing all your character rotations in terms of a rotation axis which is stored on the player. Every frame, interpolate it towards the current gravity direction, and that way the character will always stand up against their current "down". To find out which is the closed floor, shoot out raycasts in all directions every few frames, and whichever one is the shortest is deemed to be the new "down", and the gravity direction is set to the normal of the hit. This way, the character will always fall towards the nearest surface.
Answer by aldonaletto · Aug 28, 2011 at 04:31 PM
I created a rigidbody character which uses a local gravity that always points down to the surface it's on. The character adjusts its position to be always perpendicular to the ground. Take a look at it in this answer - the question is Walking on walls, but the character can walk over spheric surfaces as well.
EDITED: My script really doesn't work on a square planet - and may have problems in a spheric planet too, when the player is around the "south pole". I'm studying this case and will be back as soon as I have some conclusion (your case is really complicated!)
aldonaletto can you plz send me a copy of you gravity script its exactly what i need to add to my game i made a exact replica of google earth and that script would help me alot i just need it to keep me on the surface i have two spheres one is 60. by 60. which is the ground and the other thats on the outside of it is 80. 80. and it has no collision and its the atmospher plz help me out as soon as possible
The script is in the question http://answers.unity3d.com/questions/155907/basic-movement-walking-on-walls.html
But there's a problem: when in the "south pole", the character becomes crazy and turns 180 degrees around its local Y axis without any apparent reason. It's a problem with quaternions, and I could not find a solution yet.
Answer by ckfinite · Aug 28, 2011 at 04:44 PM
Try this (C#):
using UnityEngine;
using System.Collections;
public class GravityScript : MonoBehaviour {
public Transform gravitySource;
public float gravScalar = 4.0f;
public Vector3 rotOffset = Vector3.zero;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
var direction = gravitySource.position - transform.position;
rigidbody.AddForce(direction.normalized * gravScalar);
transform.rotation = Quaternion.LookRotation(direction.normalized)
* Quaternion.Euler(rotOffset);
}
}
Just set up rotOffset so that the model looks like it should, and it should work. If you want a exponential gravity model, that is also easily possible with this.
Thanks for the script! The only problem is the rotation doesn't work, any ideas why?
A bit of nitpicking- you should use Force$$anonymous$$ode.Acceleration in your rigidbody.AddForce function- this way it will be more gravity-like! Also you could attenuate gravity by the distance from gravitySource, so that the gravity becomes weaker as you get further and further away.
Directing the gravity to the planet center creates a problem in a cubic planet: the character is attracted to the middle of the current face, because the gravity pulls it back when trying to go to the borders (thanks God Earth is not cubic!)
Yes, because the center of the cubic face is closer to the center of the cube than the edges or corners, so the face center would always be 'downhill'...
Your answer
Follow this Question
Related Questions
Gravitational Rotation 4 Answers
Why is Quaternion.FromToRotation() not working properly? 3 Answers
Centric gravity issues... 1 Answer
Planetoid Gravity 1 Answer