Make a character dash

Hey after watching this lecture about how to make a character walk is there a way I could get my character to go into a faster dash after double tapping the walk button? I’m using the same code as in the lecture and I was wondering if there was a way to implement dashing to it.

https://answers.unity.com/questions/1192912/hot-to-make-combo-attack-1-button-do-3-things.html

It’s made for combo attacks but I’m sure it can be tweaked.

Thanks, but I can’t make it work with the cross platform input manager code. Got any other suggestions?

Hi Nicolas,

Since key input is logged only once per frame, you need at least 3 frames to check if a key was pressed down three times, once per frame. For this reason, you have to define a timeframe in which the player is supposed to press a key down x times. Store the input in an array or a List. Then you can check the array or the list if the content matches your condition for the faster dash.

I’m sorry but I don’t really get how to store the input in an array. I kind of get defining a timeframe where the player should be able to go into a dash after tapping once to walk, but even after checking the unity documentation on arrays and watching the lectures, I’m unsure how I am supposed to implement them in this situation.

here is my code:

[SerializeField] float walkSpeed = 0.5f;
[SerializeField] float dashSpeed = 1f;
bool isDashing = false;

void Update()
{
    Walk();
    FlipSprite();
}

private void Walk()
{
    if (isDashing == false)
    {
        float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal");
        Vector2 playerVelocity = new Vector2(controlThrow * walkSpeed, myRigidbody.velocity.y);
        myRigidbody.velocity = playerVelocity;


        bool playerHasHorizonalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
        myAnimator.SetBool("Walking", playerHasHorizonalSpeed);
    }
    if (isDashing == true)
    {
        float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal");
        Vector2 playerVelocity = new Vector2(controlThrow * dashSpeed, myRigidbody.velocity.y);
        myRigidbody.velocity = playerVelocity;
    

        bool playerHasHorizonalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
        myAnimator.SetBool("Running", playerHasHorizonalSpeed);
    }
}

I only included parts of it that are about horizontal movement, hence why I left out FIipSprite(), Start() and other functions that were in it. My code is very similar to Rick’s except for the fact that I included a condition for horizontal movement that changes if a bool named isDashing is true or false (the only differences are the animation and the speed, the rest stays the same).

thanks for the help.

As a novice myself I would go the simpler route of a shift click instead.

For example:

List<KeyCode> input = new List<KeyCode>();

void Update()
{
    if (Input.GetKeyDown(Enter))
    {
        input.Add(KeyCode.Enter);
    }
}

Then you could iterate over the List. If there are two KeyCode.Enter elements in the list, you could enable the dashing and remove all elements from the List. This is just one part of the solution.

This topic was automatically closed after 13 days. New replies are no longer allowed.

Privacy & Terms