Walking around a sphere - gravity?

Discussion in 'Unity Support' started by dock, Nov 21, 2008.

  1. dock

    dock

    Member

    Joined:
    Jan 2, 2008
    Messages:
    373
    I want to have a planetoid style object, and have my character walk around on the surface. Is there any way to implement gravity so that it pulls towards the centre of the sphere, rather than just downwards?

    I would prefer not to have to rotate the entire world around my character, especially as I would like to have more than one character on the surface at one time.

    Any ideas?
  2. NCarter

    NCarter

    Member

    Joined:
    Sep 3, 2005
    Messages:
    686
    Just turn off global gravity and apply a force for each object along the vector between the object itself and the planet's centre. You can use the usual equation for acceleration due to gravity if you want the force to diminish according to distance.
  3. dock

    dock

    Member

    Joined:
    Jan 2, 2008
    Messages:
    373
    Ah interesting. That sounds relatively simple... although I'll have to figure out vectors then... which still scare me!

    I suppose I should use the same vector information to re-orient the characters to always be facing 'up' as well.

    I was hoping I might be able to abuse configurable joints in order to stick the player to the surface, but some sort of gravity and re-orientation code is probably better.
  4. IPete

    IPete

    New Member

    Joined:
    May 15, 2008
    Messages:
    414
    Dock,

    Have a look at Rune's website, he has already implemented this with a sort of Mario Galaxy demo in unity.

    Let me look for the link ; here is his blog have a look half way down the page for a demo, not sure if he gives the source away somewhere. It may be worth an e-mail to him, he is a great chap, met him briefly at Unite 2008, his locomotion system demo was spectacular.

    http://runevision.com/blog/
  5. NCarter

    NCarter

    Member

    Joined:
    Sep 3, 2005
    Messages:
    686
    In the simple case, without diminishing over distance, it's just this:

    Code (csharp):
    1. rigidbody.AddForce((planet.position - transform.position).normalized * acceleration);
    Nothing to be afraid of there. ;)

    Orienting your objects so they're standing the right way up can be a bit more complex, depending on how you want it to work, but you can just use the reverse of the gravity vector as the normal that you want to align with. There are various things in the Quaternion class (such as FromToRotation) which can help you with setting up a rotation based upon this vector.
  6. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Messages:
    2,948
    Also I believe that the first edition of the unity developer magazine has an article about vectors and 3D.
  7. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Messages:
    867
    The planet walking demo is included as an example scene in the Locomotion System package at:
    http://unity3d.com/support/resources/example-projects/locomotion-ik

    However, I would not advise to use the method I use there. I simply align the character according to the normal of the surface, but this can fail badly in more complex environments. (It's more like a simulation of suction cup shoes than of custom gravity!)
  8. Charles Hinshaw

    Charles Hinshaw

    New Member

    Joined:
    Feb 6, 2008
    Messages:
    1,070
  9. dock

    dock

    Member

    Joined:
    Jan 2, 2008
    Messages:
    373
    Charles, wow - podperson's code is exactly what I need. Thank-you for finding that. I had already looked for any sort of discussion, but the search feature on this forum isn't very helpful.

    Rune, I'll be sure to take a look at your locomotion system nevertheless. Emulating suction cup shoes sounds great, haha ^_^
  10. kaedmon

    kaedmon

    New Member

    Joined:
    Jan 28, 2010
    Messages:
    25
    For any developers out there who would like to implement this really easily, try out:


    World Physics System ~ http://nimbusgarden.com/worldphysicssystem

    The World Physics System is a celestial body point gravity scripting interface for Unity, intended as a replacement for the stock downward gravity. WPS can be used to simulate planetary orbits, body-body attraction, or "snowballing" effects. You may use it to create spherical worlds "out-of-the-box," but World Physics System is implemented robustly and is lightweight, providing you complete freedom in expressing your point gravitation creativity, complex and repulsive forces, allowing you to use the system as a subcomponent for other effects, such as spells, powerups, or goal-based Artificial Intelligences for NPC's. With the WPS, the sky is truly the limit!

    Available from the Unity Asset Store, here.

    More information, here.

    Happy New Year!!!

    ~ The Nimbus Garden Team
    Last edited: Dec 31, 2012