Search Unity

2D Runner Slide Help

Discussion in '2D' started by Seremedy, May 22, 2018.

  1. Seremedy

    Seremedy

    Joined:
    May 22, 2018
    Posts:
    5
    So I've been making an endless runner game as a first project. I've gotten the running, random platforms, and object pooling so far to work fine. So I took on the next challenge of the project which is making a slide button. I get the slide to work while pressing the right button which was easy. But I don't want players to slide as long as they want. So I attempted a timer to ensure the player can slide for only a couple seconds.

    But it doesn't work...And I just started learning program in C# so I guess I need help. If someone could even give me a clue it would be much appreciated!


    Code (CSharp):
    1.   if (Input.GetKeyDown(KeyCode.DownArrow))
    2.         {
    3.             if (Grounded && Slide)
    4.             {
    5.                 SlideActivate = true;
    6.                 PlayerRigidBody.velocity = new Vector2(MoveSpeed, PlayerRigidBody.velocity.y);
    7.                 targetTime -= Time.deltaTime;
    8.  
    9.                 if (targetTime <= 2.0f)
    10.                 {
    11.                     Slide = false;
    12.                 }
    13.             }
    14.             SlideActivate = false;
    15.  
    16.             if (Slide = false)
    17.             {
    18.                 if (Grounded)
    19.                 {
    20.                     Slide = true;
    21.                 }
    22.             }
    23.         }
     
  2. 1Piotrek1

    1Piotrek1

    Joined:
    Mar 14, 2014
    Posts:
    130
    I don't see why you didn't post the full file.
    There is a lot of better quality code out here doing exactly the same, so nobody will "steal" it from you.
    You're just harming yourself.

    But from what you sent I made this.
    Basically you need to have a timer that runs when player is holding slide button and reset it when he stops.
    You can't just reset the timer right after it stops because it will just continue forever having no effect.
    You can help yourself debbuging scripts like this by logging every "if" into console and checking what happens with the variables while the game is running.
    Code (CSharp):
    1. float targetTime = 0f;
    2.     float slidingTime = 5f;// how long can player slide
    3.  
    4.     public void Update()
    5.     {
    6.         if (Input.GetKeyDown(KeyCode.DownArrow))
    7.         {
    8.             if (Grounded && targetTime > 0f)
    9.             {
    10.                 Sliding = true;
    11.                 PlayerRigidBody.velocity = new Vector2(MoveSpeed, PlayerRigidBody.velocity.y);
    12.                 targetTime -= Time.deltaTime;
    13.             }
    14.             else
    15.             {
    16.                 // I'm guessing that this boolean is might be used for something like animation, else it's unnecessary
    17.                 Sliding = false;
    18.             }
    19.         }
    20.         else
    21.         {
    22.             // if player stopped holding the button replenish the sliding time
    23.             // you might want to make it replenish gradually
    24.             Sliding = false;
    25.             targetTime = slidingTime;
    26.         }
    27.     }
    Obviously the code isn't tested, I won't write the entire class just for a simple question.
     
    Seremedy likes this.