Hey guys!
I am creating a 3D game and I want it to dash forward when you right-click.
I am using rigidbody to move, not a char controller or anything.
I have movement perfect and when I try to do this:
It works really well, and is basically what I need. HOWEVER, that works perfectly when I am standing still, but when I am already walking and do it, it really sucks… It likes pushes me a millimeter forward haha with no more slide or anything. How can I make this work for when not walking and when walking? Thanks!
Are you setting Rigidbody.velocity to control your character? If so, this will end up overriding any forces that you apply to the rigidbody, since it’s updated every frame. If this is true, then controlling the character with forces instead of setting velocity is probably the way to go.
Okay, so I am using the velocity for movement.
Sooo then, I need to do something with velocity and not the AddForce then for my rigidbody? Ooorr how will this work then?
The way I see it, there are two main ways of doing this:
A: Control your character movements with forces instead of modifying the rigidbody’s velocity. This way, additional forces will be additive to the control force. Each frame, all forces contribute toward the rigidbody’s velocity, which is calculated by the physics engine, and there is nothing in your script constantly resetting the velocity to some default value.
B: Script your own additive velocity system in your character’s controller script. For example, declare a few Vector3 variables that all individually contribute to your character’s final velocity, such as controlVelocity, dashVelocity, pushVelocity, etc. Come up with methods to calculate what each of these should be individually (controlVelocity is based on arrow key/control stick input, dashVelocity is based on mouse button state, pushVelocity is set from an enemy script’s OnCollisionEnter() function; something like that) and add them all together at the end with
You’ll have to add a few steps to decay non-controlled velocities over time, otherwise they will be active for one frame, or just forever.
A will give you more realistic motion and allows use of the physics engine’s forces, but it can be sloppy at times if not coded carefully. Needed for games that rely on physics for bumping, rotation, etc.
B gives you more precise motions, but limits use of forces to when the above statement is disabled, and it won’t look particularly realistic. Works well for platformer games, or other games where the character doesn’t really care about physics. I’ve done this method myself for a 2D game.