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

Question How to recreate MMX/MMZ dash?

Discussion in '2D' started by IkarusBR, Jul 15, 2023.

  1. IkarusBR

    IkarusBR

    Joined:
    May 12, 2023
    Posts:
    8
    I tried to implement a Megaman-X-like dash in my code a few times but failed miserably. I watched one specific tutorial I found on the youtube but it didn't work, too. Took a few step backs and tried to breakdown how a Megaman X dash really works.

    I got this:
    • when you press the dash button without directional influence, you’re sent to the direction you’re looking;
    • the dash works like a “better jumping”, as in whenever you hold it, it applies a force to the desired direction. however after a period of time/releasing the dash button, it stops. literally like a normal “mario jump” but with horizontal movement.
    • the dash cant be done on the air
    • your speed increases whenever you dash (duh)
    • you can “spam dash” [though i think if you do it your speed decreases a little?]
    • you can quickly change the direction of your dash with directional input, even when you are dashing
      though I’ve been struggling a little to get this one down to code.
    I’d love to ask two questions, one is how to become better at this “second stage”, that is, turning your “breakdown” into actual code, and if you guys can help me out with some guidance. It would be really appreciated. Just for the sake of it, here's the previous code where I attempted doing the dash:

    Code (CSharp):
    1.   void Dash()
    2.     {
    3.         isDashing = true;
    4.         if (isDashing && !Input.GetKey(KeyCode.LeftShift)) // if the player is dashing but not pressing the dash button
    5.         {
    6.             rb.velocity -= new Vector2 (mov*lowDashMult*Time.deltaTime, 0); //decrease his horizontal speed according to the direction they're pressing, time and "lowDashMult"
    7.         }
    8.         rb.velocity = new Vector2 (dashSpeed*speed, rb.velocity.y); //actually apply speed
    9.         StartCoroutine(StopDash()); //easier way i've found to make unity wait for seconds
    10.     }
    11.     IEnumerator StopDash()
    12.     {
    13.         yield return new WaitForSeconds(dashDuration);
    14.         isDashing = false;
    15.         yield return new WaitForSeconds(dashCooldown);
    16.     }
    17. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    First, let me salute your excellent decision to break it down. Simply doing that gives you a HUGE advantage in trying to achieve what you want. Nice!

    Second, again let me salute you for asking this question:

    This is the Magic Sauce that sets a great developer apart from a regular developer. It is really a skill that you only develop over time by repetition in many contexts and situations. But the fact that you are methodically decomposing something like a dash and making yourself a shopping list will again give you a huge leg up on it.

    Finally, that huge shopping list, as great as it is, will never be completed "all at once." You will want to work at it iteratively. Get the move left/right and jump stuff going exactly how you want and commit the code to source control so you can begin experimenting with the next steps.

    Specifically, I would not use a coroutine to do this but instead simply a float variable used as a cooldown timer. Then you simply choose to either move normally if the timer is zero, or move dashed-ly if the timer is nonzero. Of course you count down the timer and set it to the dash time when dash is pressed.

    Essentially you would have two slightly-separate code paths for motion: normal and dashing, controlled by the timer.

    For instance, just a standard "cooldown timer" mechanism could directly be used:

    Cooldown timers, gun bullet intervals, shot spacing, rate of fire:

    https://forum.unity.com/threads/fire-rate-issues.1026154/#post-6646297
     
  3. IkarusBR

    IkarusBR

    Joined:
    May 12, 2023
    Posts:
    8
    thank you so much! i'm actually really honoured to be saluted that way, really, thank you <3
    about your idea, it makes sense. will try to experiment with it. just, since we're at it, should i make the dash function in the fixedUpdate or in the Update method? i've always heard that physics stuff should be put on the FixedUpdate, though i'm not sure where the dash fits best...
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    NeilB133 likes this.