Search Unity

Different gravity areas or character "sticking" to different surfaces?

Discussion in '2D' started by Flybye, Mar 27, 2015.

  1. Flybye

    Flybye

    Joined:
    Mar 25, 2015
    Posts:
    100
    So imagine you have a room with a ceiling and a floor. Is it possible to make gravity on the ceiling and floor? Lets say if you jump half way up, you end up "sticking" to the ceiling and be able to walk back and fourth.

    And same thing on ceilng. If you jump half way from the ceilling you end up on the floor

    Also would it be possible to make an area on the floor with no gravity? So with same rules above on gravity. If I walk on this area, the character instantly is moved to the ceiling.

    I know its possible to trigger different gravities, but I would like to have both gravities existing. Im planning my first game and am hoping Im not going over my head with certain things. :)
     
    Last edited: Mar 27, 2015
  2. MoonJellyGames

    MoonJellyGames

    Joined:
    Oct 2, 2014
    Posts:
    331
    You could try using BoxCollider2D colliders with "IsTrigger" checked in the inspector. Give it a script (GravityArea, or something) like this:

    Code (CSharp):
    1. // flip the gravity to walk on ceilings
    2. public void OnTriggerEnter2D(Collider2D obj)
    3. {
    4.      if (obj.rigidbody2D != null)
    5.      {
    6.           obj.rigidbody2D.gravityScale = -1;
    7.      {
    8. }
    9. // flip gravity back to normal
    10. public void OnTriggerExit2D(Collider2D obj)
    11. {
    12.           if (obj.rigidbody2D != null)
    13.      {
    14.           obj.rigidbody2D.gravityScale = -1;
    15.      {
    16. }
    That will flip the gravity. You'll probably need to change more things on the player object though. Jumping will require that you multiply it's jump force (however you're handling that) by two. Also, how are you handling floor detection? Raycasts? That will require some work too.

    This is a play mechanic that I've also considered working into my 2D platformer/puzzle. It's basically the same as what they do in those side-scrolling sections of Super Mario Galaxy.
     
  3. Flybye

    Flybye

    Joined:
    Mar 25, 2015
    Posts:
    100
    Thx! Im stil not sure on floor detection or raycast as I am still very new. Im trying to gauge my first project to see if Im in over my head, but I dont believe so. Once the mechabics are figured out, everything past that is all straight forward with graphical work and level design.

    What about putting gravity on a side wall? Is that possible?
     
    Last edited: Mar 28, 2015
  4. MoonJellyGames

    MoonJellyGames

    Joined:
    Oct 2, 2014
    Posts:
    331
    Haha you know, I was planning on including this "gravity field" mechanic in my game too, but I decided that it might be too complicated. I don't know what other play mechanics you have planned for your game, so whether or not this will be too challenging for a beginner really depends. I'm also quite new to this. :)

    The horizontal gravity is something I played around with too. You can change the world gravity settings by going into Edit > Project Settings > Physics

    Right at the top, you can set gravity values for x, y, and z. By default, it should be a negative y-value, of course. The question is, can you change the gravity axis within an area and I don't have the answer for that.