Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help with making player 'glide'

Discussion in '2D' started by PokeHer, Aug 1, 2014.

  1. PokeHer

    PokeHer

    Joined:
    Feb 26, 2014
    Posts:
    2
    Hi. I'm looking for a way to make my player glide on request. For example, consider a normal platform type game, my player jumps using:

    Code (CSharp):
    1.     private void Jump() {
    2.         if (!IsGrounded())
    3.             return;
    4.  
    5.         rigidbody2D.AddForce(Vector2.up * JumpHeight);
    6.     }
    This creates a simple jump effect like this:



    When the user holds a button, I want an effect similar to this:



    How can I achieve this? I've played around with various AddForces to no avail.
    For info, I have a GroundCheck object which sits beneath the player to help me determine if the player is grounded so if they are able to jump. I am thinking I need another object called 'GlideCheck' which sits even lower beneath the player so that I can determine if the player is able to glide. The player will be able to glide only on the way down and only if not grounded. Those conditions I'm fine with, it's just the glide effect I'm struggling with.

    Any help will be appreciated.

    Many Thanks :)
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Well, while the player is holding the glide button, you could modify the `gravity` parameter on the player's collider, so that it becomes less heavy (or change the mass?) ... which would result in it falling slower? Or, apply a small upward force? Or, cap the vertical velocity value at a maximum.
     
    PokeHer likes this.
  3. PokeHer

    PokeHer

    Joined:
    Feb 26, 2014
    Posts:
    2
    Great idea with the gravity. I tried this before a while back but I don't think I had the negative y velocity condition on so it would appear to not work! I have tried just now and it's looking more like it!

    Thank you sir!

    [UPDATE] So it needs tweaking but this is the general idea:

    Code (CSharp):
    1.         if (CanGlide() && rigidbody2D.velocity.y < 0) {
    2.             rigidbody2D.gravityScale = (float)(0.5 / -rigidbody2D.velocity.y);
    3.             playerMoveScript.speed += SpeedIncrementer;
    4.         }
    Thanks again
     
    Last edited: Aug 1, 2014