x


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!

more ▼

asked Aug 28, 2011 at 03:57 PM

logty gravatar image

logty
255 91 64 70

Sorry, I need to clarify a little bit (this might make it a little more complicated) My 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

Aug 28, 2011 at 05:30 PM logty
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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!)

more ▼

answered Aug 28, 2011 at 04:31 PM

aldonaletto gravatar image

aldonaletto
57k 31 78 291

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

Jan 13, 2012 at 11:41 PM kidrockkenny

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.

Jan 14, 2012 at 05:01 AM aldonaletto
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Aug 28, 2011 at 04:44 PM

ckfinite gravatar image

ckfinite
1.4k 21 17 37

Thanks for the script! The only problem is the rotation doesn't work, any ideas why?

Sep 14, 2011 at 11:08 AM logty

how should i set up RotOffset?

Sep 14, 2011 at 11:11 AM logty

A bit of nitpicking- you should use ForceMode.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.

Sep 14, 2011 at 11:14 AM syclamoth

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!)

Sep 14, 2011 at 02:30 PM aldonaletto

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'...

Jan 14, 2012 at 06:37 AM Tasarran
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Sep 14, 2011 at 11:19 AM

syclamoth gravatar image

syclamoth
18.4k 19 26 111

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3602
x775
x104

asked: Aug 28, 2011 at 03:57 PM

Seen: 4226 times

Last Updated: Jan 14, 2012 at 06:37 AM