(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!
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.
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?
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:
Perform dash. (Dash → maintaining set velocity in given direction for X seconds)
Check if you’re on ground.
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.
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.
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.AddForce(transform.forward * dashDistance);
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.
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:
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.
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?
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.