Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Walking around a sphere - gravity?

Discussion in 'Editor & General Support' started by dock, Nov 21, 2008.

  1. dock

    dock

    Joined:
    Jan 2, 2008
    Posts:
    603
    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?
     
    bhoffman67 likes this.
  2. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    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

    Joined:
    Jan 2, 2008
    Posts:
    603
    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

    Joined:
    May 15, 2008
    Posts:
    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

    Joined:
    Sep 3, 2005
    Posts:
    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

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

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    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

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

    dock

    Joined:
    Jan 2, 2008
    Posts:
    603
    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

    Joined:
    Jan 28, 2010
    Posts:
    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
    bhoffman67 likes this.
  11. Pommimonni

    Pommimonni

    Joined:
    Sep 18, 2013
    Posts:
    2
    This should work

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class SphericalGravity : MonoBehaviour {
    6.  
    7.     public List<GameObject> objects;
    8.     public GameObject planet;
    9.  
    10.     public float gravitationalPull;
    11.  
    12.     void FixedUpdate() {
    13.         //apply spherical gravity to selected objects (set the objects in editor)
    14.         foreach (GameObject o in objects) {
    15.             if(o.rigidbody){
    16.                 o.rigidbody.AddForce((planet.transform.position - o.transform.position).normalized * gravitationalPull);
    17.             }
    18.         }
    19.         //or apply gravity to all game objects with rigidbody
    20.         foreach (GameObject o in UnityEngine.Object.FindObjectsOfType<GameObject>()) {
    21.             if(o.rigidbody && o != planet){
    22.                 o.rigidbody.AddForce((planet.transform.position - o.transform.position).normalized * gravitationalPull);
    23.             }
    24.         }
    25.     }
    26.  
    27. }
    28.  
     
    RomanSelikhov and Lorethem like this.
  12. simulism

    simulism

    Joined:
    May 11, 2016
    Posts:
    14
  13. Sawney

    Sawney

    Joined:
    Dec 15, 2016
    Posts:
    1
    Made a tutorial:
     
    Lorethem and kjbenoit2 like this.
  14. kjbenoit2

    kjbenoit2

    Joined:
    Aug 25, 2018
    Posts:
    1
    think you Sawney and Pommimonni i'ts perfect for me
     
  15. ramsey05

    ramsey05

    Joined:
    Jan 18, 2020
    Posts:
    25