I want to create a (very small) spherical planet that my character could walk around. This is a problem right now since, obviously, once my character gets to the side of the planet… he falls off!
Is there a way in Unity to create a centre of gravity as being “inside” an object (ie: my planet), so that every object wall inward into it, as opposed to fall of its face.
here’s an example:
The only solution I’ve thought up so far is to have a bit of unicycle-like system. By this I mean, whenever you walk on the surface of the planet, the planet rotates the opposite direction so that you, as a player, are always on the TOP of the planet. This would work in theory, but any physics-objects from the other side of the planet would fall off the surface as that side of the planet became too far angled down…
I am definitely interested in this, also. I want to explore concepts of being inside a sphere with gravity everywhere outwards, and also a ‘ringworld’ wher gravity goes outward.
Try having your rigidbody’s gravity turned off, then having a localized force attached to your game object. Then rotate your character based on the normal of the ground ( detected with a Collision ).
Thinking again about the problem… the movement control could be moving the planet under the motionless player. “Moving the character” would rotate the planet around it’s center. “turning the player” would turn the planet, etc.
of course you’d have to move EVERYTHING, including skydome, or skybox, etc. could be slow.
It’s a workaround, and won’t work for my player-on-the-inside-of-a-sphere.
My technique is to create my own localized gravity based on the closest gravity object and then rotate every frame based on the current gravity vector. It’s a sphere, so you can just move towards the center of the sphere.
var myGravity : Transform;
var gravityVal : float = 9.8;
function Update()
{
rigidbody.velocity += gravityVal * (myGravity.position - transform.position).normalized;
var myY : float = transform.eulerAngles.y;
transform.LookAt(transform.position + myGravity.position);
transform.eulerAngles = Vector3(transform.eulerAngles.x + 90, myY, transform.eulerAngles.z);
}
Something like the above. It’s worked for me so far. This was only used to test a prototype, so it isn’t fully tested. Should still move you towards the center of the planet and keep you oriented with your feet towards the ground, though.
And apply the opposite if you’re inside the sphere… very pretty solution. Like it. I’m new to scripting, though, so could you (or someone) post the final nugget of code?
I actually just applied force in every Update() from an array of planet objects when doing this. It sounds expensive, but runs pretty well. For my work I just find the unit vector between a physics object and each gravity source every update and multiply it by my object’s mass and the planet’s set gravity force. If you wanted to be more realistic, you’d probably want to divide that all by distance^2 (I actually thought that was what I was doing until checking my code just now :roll:).
You can AddExplosionForce() in the center of your sphere with a negative force, it works for me, but i did not care about the rotations beacause i am making a racing game.
Actually, it will not be like mario galaxy because the gravity will decrease if you go higher ( like in real world).
Also, you can disable gravity for your character and add a custom force. Then raycast downwards for the very first mesh plane and put the negative of its normal to your custom force.