Search Unity

Anti-gravity mechanic

Discussion in 'Physics' started by Arandomone, Mar 26, 2015.

  1. Arandomone

    Arandomone

    Joined:
    Mar 26, 2015
    Posts:
    3
    Hi,

    I am currently starting a prototype, and I'd like to include a gameplay mechanic that'd allow the player to switch off the game's gravity (affecting only the playable character) and switch it on again.
    OR, to allow the player to walk on the walls of a room (kind of what you'd see in Mario Galaxy in fact), of course, the action would be controllable via a key that the player can press at any time. For example : the character can only walk on the ground, then the player presses a key , and can now walk on the room's walls, then releases the key, going back to normal.

    I'm a beginner in Unity, so I don't have a clue to how to do that kind of thing.
    Any help would be greatly appreciated, thanks ^^
     
  2. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    What exactly are you using to control the character? A rigidbody, character controller, what?
     
  3. Arandomone

    Arandomone

    Joined:
    Mar 26, 2015
    Posts:
    3
    a simple prefab first person controller.

    It's a really basic prototype
     
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,507
    Disable gravity at your character's rigidbody and use something like this in FixedUpdate:

    Code (CSharp):
    1. GetComponent<rigidbody>().AddForce(gravity_direction * 9.81f, ForceMode.Acceleration);
    where "gravity_direction" is a normalized Vector3.
     
    xpath likes this.
  5. Arandomone

    Arandomone

    Joined:
    Mar 26, 2015
    Posts:
    3
    allright I'll give it a try, thank you