# How do I make a proper dash mechanic in a 3rd person game?

(Using new unity input system with a game controller)

I am trying to add a dash mechanic for my 3rd person game which will allow you to dash in any direction that your character is moving/facing similar to Bloodborne or Nier Automata. This code I have so far is working in the sense that it does in fact move the player forward in the direction that they are moving but the problem now is that if the player chooses to move in the opposite direction after the dash they will obviously be fighting against the applied force.

How can I have the character dash and cancel out the force if the player decides to move back in the opposite direction after the dash is done? Also, I would like to know how to make the character dash only a certain distance and then stop as soon as they finish as well as set a certain speed that determines how fast the player moves along the screen when they dash. Even if the mechanic basically makes the character instantaneously move from one point to another, any help is appreciated. Thank you!

void Dash()
{
rb.velocity = transform.forward * dashSpeed;
}

I donâ€™t think physics based movement is the right tool for the kind of movement behavior youâ€™re going for. It is not good for making precise time based movement mechanics.

1 Like

Are you using root motion on your animations? If so You can override this to apply a speed factor in OnAnimtorMove.

You can always just set the rigidbodyâ€™s velocity to 0, then apply force in the new direction.

I am not yet. I am currently in the early stages so all I have are primitive objects and so Iâ€™m trying to get Gameplay down first before I get into animations. With that said, what do you think?

So what do you think I should do instead? What else is there other than physics? (note, fairly new unity user)

Oh I see. How would you express that in code because Iâ€™d imagine it would start something like

rb.velocity = 0

but how would I express force in a new direction especially when I hit the dash button?

The main options besides rigidbody movement are manipulating the transform or a CharacterController. Both offer more directly controlled but less realistic motion (realistic in the sense of forces, drag, friction, etc). A dash mechanic doesnâ€™t sound like youâ€™re going for realism though.

Yeah, not at all lol I see so currently I have a rigidbody but does this mean I should rid of it and have a character controller instead or should I just stick with the rigidbody and figure out a transform? Because if I keep it the way it is, arenâ€™t I already using a transform in my code?

Usually, in games, when the dash is performed, for the duration of dash any other directional movement is disabled.

Regarding cancelling movement. Regardless of what you do, your character is going to have maximum movement speed when on ground. This limit is going to be included in your controller even when youâ€™re using rigidbodies, and the code will prevent character from going any faster when the velocity limit is reached.

So. That limiter will take care of slowdown automatically when the dash is over.

Additionally you can simply code forced slowdown in. It would go like this:

1. Perform dash. (Dash â†’ maintaining set velocity in given direction for X seconds)
2. Check if youâ€™re on ground.
3. If youâ€™re on ground, cap ground movement speed. Meaning ensure that length of rigidbody GROUND velocity vector is not higher maximum allowed velocity.

That would pretty much do it.

To calculate ground velocity, project velocity vector onto ground plane using Vector3.ProjectOnPlane or manually.

However, it is probably the best to have slowdown handled by movement speed limiter in your controller, because that would allow fun stuff like using dash on ice field to get higher speed.

Thank you for your input! I wonâ€™t lie, itâ€™ll take me a little bit of time to code this in the right way but I appreciate your help!

velocity is in 3D space, so itâ€™s a Vector3. It would actually be=
`rb.velocity = Vector3.zero;`
Vector3.zero is a handy constant thatâ€™s just a Vector3 with x=0, y=0, and z=0.

Then to express force in a new direction, youâ€™d use AddForce

Thank you for your answer back. I tried to do it but I run into the issue that my character then doesnâ€™t move at all when I add it in. This is what it looks like so far:

void Dash()
{
rb.velocity = Vector3.zero;
}
I have even tried this line of code but it really didnâ€™t make much of an improvement:
rb.velocity = transform.TransformDirection(transform.forward) * dashDistance;

I guess what I am getting at is that while these codes seem to work well in moving the player forward and I appreciate everyones wonderful advice especially the response from neginfinity my issue is still getting the player to move just a certain distance in the 3d space as well as being able to stop the motion as soon as it finishes. I am having a really hard time just trying to figure that out.

Have you tried reversing the order of these two lines? Unless AddForce is somehow asynchronous, setting the velocity to zero will cancel it out.

I did try it before but it did the same thing. Now though I think I might have figured out what may be the issue. Currently I am using a capsule as the primitive object for my player but I just now noticed that I have XYZ rotations frozen in the editor. I took them off and the code worked differently then how it did before.I also tried changing the object from the capsule to cube with the box collider and that also was different.

I am going to work so I have to stop using Unity but I am going to try messing around with Unity more and give an update if maybe the freeze rotations have something to do with why I am having such a hard time. While I am gone if you can reply, do you think maybe the rotation freezes could be a big reason as to why nothing is really working properly? I figured why not freeze them since my character shouldnâ€™t be rotating around the screen, you know?

Having 3d rotations frozen on player controller is kinda the point. It is done so your controller wonâ€™t tip over, and so being hit by rigidbodies wonâ€™t make player spin.

Yeah, youâ€™re right. I guess I just got really excited when I saw a possible light of hope lol Well I have tried quite a few different things to get it working accordingly with my code and I just donâ€™t have any real luck. As of now this is what I am using to at least move my character forward:

void Dash()
{
transform.Translate(Vector3.forward * dashDistance * Time.deltaTime);
}

I have spent a while on this and I feel I should move on and just revisit this once I get much further into development. Thank you everyone for your replies, feel free to post on here if anyone ever wants to revisit the idea.

Thatâ€™s actually wrong.

By moving this way you are very likely to skip collision detection and tunnel through walls and other objects, plus you arenâ€™t adding momentum to your rigidbodies properly.

I suggest to take unityâ€™s first person controller, tear it apart, learn how it works, then implement your own walking controller from scratch. Using rigidbodies and without them.

Then once you understand how it works, you should be able to implement dashing mechanic properly.

Yeah, youâ€™re right. Thatâ€™s exactly what happened but I guess I was okay with at least having a place holder for the mechanic until I started working on it again. I am going to keep trying to figure out the dash mechanic but for now Iâ€™ll work on it here and there. Iâ€™ll make sure to do that though, I still have a lot to learn with Unity. Thank you

(Edit)
Actually, now that I look I donâ€™t see a first person controller. Do you mean learn making a first person controller through tutorials or did I miss something in Unity?

Hmm. First/Third Person controllers should be part of unity standard assetsâ€¦
Here they are:
https://assetstore.unity.com/packages/essentials/asset-packs/standard-assets-for-unity-2017-3-32351

Actually it feels that Iâ€™m a bit out of the loop here, as it seems they might be phased out? You could try to study them with older unity version, or you could try poking forums for a replacement.