Search Unity

Mario Galaxy-like circular gravity?

Discussion in 'Editor & General Support' started by Rainville, Oct 30, 2009.

  1. Rainville

    Rainville

    Joined:
    Aug 18, 2009
    Posts:
    45
    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:
    http://www.youtube.com/watch?v=id1FWB5Oyho

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

    Can anyone think of any other solutions?
     
  2. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    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.

    Any ideas?
     
  3. Rainville

    Rainville

    Joined:
    Aug 18, 2009
    Posts:
    45
    Vimalakirti was a pretty awesome dude :)
     
  4. Lab013

    Lab013

    Joined:
    Oct 22, 2008
    Posts:
    405
    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 ).
     
  5. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    "How do we develop games, when there is nothing to be developed?
    "What code is written, without a writer?
    "What game is played, without a player?"

    Thus he spoke.
     
  6. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    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.
     
  7. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    Lab013, could you elaborate? Tell a little more about which scripting classes would be used? just a hint?
     
  8. Lab013

    Lab013

    Joined:
    Oct 22, 2008
    Posts:
    405
  9. bobber205

    bobber205

    Joined:
    May 12, 2009
    Posts:
    139
  10. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,572
    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.

    Code (csharp):
    1. var myGravity : Transform;
    2.  
    3. var gravityVal : float = 9.8;
    4.  
    5. function Update()
    6. {
    7.    rigidbody.velocity += gravityVal * (myGravity.position - transform.position).normalized;
    8.    var myY : float = transform.eulerAngles.y;
    9.    transform.LookAt(transform.position + myGravity.position);
    10.    transform.eulerAngles = Vector3(transform.eulerAngles.x + 90, myY, transform.eulerAngles.z);
    11. }
    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.
     
  11. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    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?
     
  12. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,572
    Just subtract ninety instead of adding ninety and make it transform.position - myGravity.position instead of the other way around. It should work.
     
  13. TyrusPeace

    TyrusPeace

    Joined:
    Jun 30, 2009
    Posts:
    36
    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:).

    http://krackalack.com/planets-test.html
    (you'll want to shoot a couple hundred cubes so you can get the idea :p)
     
  14. marceloroca

    marceloroca

    Joined:
    Feb 24, 2009
    Posts:
    53
  15. ZoomDomain

    ZoomDomain

    Joined:
    Aug 30, 2012
    Posts:
    150
    I can add that that this doesnt work together with character motor movement?!
     
  16. Sagi02

    Sagi02

    Joined:
    Feb 6, 2015
    Posts:
    5
    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).
     
  17. Sagi02

    Sagi02

    Joined:
    Feb 6, 2015
    Posts:
    5
    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.