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

Adding a Glide to my 2d platformer.

Discussion in '2D' started by mediocrecreampuff, Aug 10, 2020.

  1. mediocrecreampuff

    mediocrecreampuff

    Joined:
    Jan 6, 2020
    Posts:
    9
    Im trying to add a glide to my character after he double jumps.

    This is my code for double jumping which i got from blackthornprods tutorial on it.(Modified a little)
    if (isGrounded)
    {
    extraJumps = extraJumpsValue;
    }

    if (Input.GetKeyDown("space")&& isGrounded)
    {
    rb2d.velocity = Vector2.up * jumpForce;


    }else if (Input.GetKeyDown("space") | && extraJumps > 0)
    {
    rb2d.velocity = Vector2.up * jumpForce;
    extraJumps--;

    }
    This is my code for gliding.
    if(Input.GetKey("space")&&!isGrounded&&extraJumps==0)
    {
    rb2d.velocity =new Vector2(rb2d.velocity.x, rb2d.velocity.y*glideValue);
    }
    It doesn't activate at all in this state. It kinda works if I attach it to the jump code with an 'else' but i would have to press space three times in order to glide and it feels off. I just want to glide after space is pressed the second time. If anyone knows what I'm doing wrong i would appreciate any help.
     
  2. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    You likely could make it much simpler by checking for space key when not grounded then set gravity to a lower fixed amount.
     
  3. mediocrecreampuff

    mediocrecreampuff

    Joined:
    Jan 6, 2020
    Posts:
    9
    Thanks I got it working.