# Walking around a sphere - gravity?

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?

1 Like

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.

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.

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/

In the simple case, without diminishing over distance, it’s just this:

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

Also I believe that the first edition of the unity developer magazine has an article about vectors and 3D.

The planet walking demo is included as an example scene in the Locomotion System package at:

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

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

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.

Happy New Year!!!

~ The Nimbus Garden Team

1 Like

This should work

``````using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SphericalGravity : MonoBehaviour {

public List<GameObject> objects;
public GameObject planet;

public float gravitationalPull;

void FixedUpdate() {
//apply spherical gravity to selected objects (set the objects in editor)
foreach (GameObject o in objects) {
if(o.rigidbody){
}
}
//or apply gravity to all game objects with rigidbody
foreach (GameObject o in UnityEngine.Object.FindObjectsOfType<GameObject>()) {
if(o.rigidbody && o != planet){
}
}
}

}
``````
2 Likes

Thank you, Pommimonni, that works perfectly.

2 Likes

think you Sawney and Pommimonni i’ts perfect for me

yey