Dash function problems

Hello all, total beginner here.

I’m having difficulties implementing a function I want. I have a character that can run around and jump, and I’m trying to code the ability to do a dash in the direction where the mouse swipes. The dash function works, here is the code:

   private void OnCollisionEnter2D(Collision2D collision)
    {
        isPlayerTouchingLevel = true;
    }
    private void OnCollisionExit2D(Collision2D collision)
    {
        isPlayerTouchingLevel = false;
    }
    private void DetectMouseMovement()
    {
        float mouseXMovement = Input.GetAxis("Mouse X");
        float mouseYMovement = Input.GetAxis("Mouse Y");

        if (mouseXMovement != 0 || mouseYMovement != 0)
        {
            newMousePosition = Input.mousePosition;
            mouseDirection = newMousePosition - mousePosition;
            mousePosition = newMousePosition;
            mouseMoved = true;
            Dash();
        }
    }
    private void Dash()
    {
        if(mouseMoved && !isPlayerTouchingLevel)
        {
            playerRigidBody.AddForce(mouseDirection, ForceMode2D.Force);
        }
    }

This works fine, but as long as I’m not touching the ground and the mouse moves I keep dashing. I would like to be able to dash once and then reset after collision. I’ve tried a lot of different approaches but I can’t seem to get it to work properly. Any ideas ?

Thanks in advance.

Sam

Hi and welcome,

Currently you are calling Dash() whenever you detect some mouse movement. As you said, you dont want to dash when you are already dashing, for example. To prevent this, simply introduce a new bool “isDashing”, which you set true in Dash(), false when you are not dashing anymore, and can then use it in your if-statement inside of DetectMouseMovement, since you only want to execute dash if(!isDashing).

That said, you probably want to use ForceMode.Impulse for a dash. Also, detecting a collision with the ground is probably not a very good indicator for whether you are still dashing or not - so i’d probably base it on your current velocity or something along those lines instead.

Hope this helps.

Edit:
Or did you mean “reset after collision” as in stopping the dash momentum, similar to how friction would slow you down?

Thank you for the reply Yoreki.

I have tried a similar solution to yours, but the problem is ‘isDashing’ would turn true after the slightest of mouse movement, which almost disables the function to dash completely. I’ve tried my way working around that but with no effect until now.

I figured one dash which sets the bool to true followed by the bool set to false when colliding. This is because I want to be able to dash after a jump, and to be able to jump I need to touch a collider. How would you suggest implementing this with velocity ?

Thanks again.

Yes, because you currently just apply a normal force, not an impulse. You would only need to apply an impulse once.

Do you want to be able to dash after a jump, or only be able to dash after a jump? If you want to be able to dash after jumping (but not only then), then that basically means you can always dash, which is the assumption i started with.
I’m assuming you have some code that makes you move according to a specific speed while walking. So to figure out if you are dashing, you could most likely just check if your velocity.x is higher than the usual walking / running velocity, or something like that. This depends on what exactly you are going for, and is honestly more of a design question than a technical one, since there are probably a million ways to implement a dash.
While we are at it: did you look up some tutorials? Especially for platformer-like movement, there are tons of Unity tutorials on youtube. And i’m pretty sure you will find something for slightly more advanced movement options, like dashes, as well.

Alright, thank you. I will do more research on the different forces, I tried impulse but the force applied was too much, therefore I tried an alternative.

The only times I’m not going to be touching a collider is when I’m jumping or running off of something. Which means that being able to always dash sounds like the plan unless I’m already performing one.

I just started all of this and I’m trying to refrain from watching too many prepped solutions and copying peoples existing work. Breaking my head over things and researching will make it stick faster.

I will try to implement your take on this, thanks again : ).

Sam

I can imagine, since you basically applied it every frame the mouse moved, making your character fly off like some rocket hehe. Imagine it like that, “force” is the default, you use it for stuff like gravity or other accelerations. An “impulse” applies the force all at once, which you would use for jumping, making stuff fly off near explosions, or to implement your dash.

Hope this helps point you in the right direction.
And i hope it explains why there is a difference between the two types of force in the first place.

I agree, but at the same time i dont. I’m always in support of people who try to really understand what they are doing, instead of just looking for a solution to copy&paste. However, it can also help to see how others did it, especially if you are just getting started. This gives you a general idea, you see lots of neat tricks you did not search for, simply by following beginner/tutorial series on youtube, and if you manually type the code down (dont copy it) you still learn a lot, especially if it’s a good tutorial that explains what each part is for. This enables you to then go ahead and experiment around afterwards. And in most cases that’s how i’d recomment people to try and learn new things.
So as an analogy, if you want to create a car from scratch, then it’s probably not helping if you start by learning how to make your own rubber for the wheels. Instead, take the knowledge of others and make it your own and improve on it.

Of course that’s just my opinion, so feel free to learn in any way you prefer :slight_smile:

It does, thanks for explaining everything clearly.

I understand what you mean 100%.I completed a course before I started to get the basics down, but I will always try to do things first myself unless I draw a blank. By the time I get stuck I start to look up solutions but most of the time the framework will already be done. I might find a good mix between the two approaches :).

Sam

1 Like

This way you will quickly get into a mess of booleans and ifs wich is hard to maintain and extend, check this pattern it helps to manage objects with multiple states State · Design Patterns Revisited · Game Programming Patterns

Thank you, I will look into this.