Can't figure out adding instead of setting velocity for a first person char controller

Godot Version

4.3

Question

So I’m trying to make a momentum based movement system, (and am quite new to godot and gdscript). It is essentially the same as the default movement controller, except instead of directly setting the velocity to the (direction * speed) I would like to add it on top of whatever other velocity I already have. This is so that using normal movement doesn’t overwrite what momentum may have been built up. Currently, I do not have anything else programmed in that changes the velocity, but I’m taking care of this now so theres room to implement that later without going back and rewriting everything.

Ideally when i begin pressing an input key, a flat value should be added to the velocity. Releasing the key will revert the change by the same amount, even if the total velocity has changed since then.

I’ve gotten the first bit fine for the most part, Input.is_action_just_pressed() is easy enough, however the issue for me is running back that value when the key is released. Ive tried quite a few different ways. One was attempting to “catch” the last direction value and subtract it multiplied by speed from velocity when it detected a key release. Another involved putting each direction vector into an array as the keys are held down, then adding everything in the array together and normalizing it for a flat increase (i had removed the .normalized() from the direction variable in this one because i thought the decimal values would be weird to work with), but from there i didn’t manage to figure out anything good. In every single attempt, releasing a key when multiple are pressed at once yielded the wrong direction, or more accurately a weird diagonal direction, most likely from the combined direction value. Any ideas?

The template as of 4.3 almost gets this right. It does use move_toward when there is not direction pressed, but it needs to include delta and a acceleration value.

I believe a better template is due to be merged soon, maybe not 4.4 though

You can see this template makes use of momentum based movement while remaining easy to understand.

Thanks for the reply. It definitely seems like it could be closer.

Though, even with acceleration and delta, doesn’t move_toward still shift the whole horizontal velocity to whatever base movement speed you may have? I may just not be fully grasping line 36.

Lets say i have an x velocity of 20 at the time i’m trying to start moving in the same direction, and a base movement speed of about 10. Since its doing velocity = horizontal velocity, wouldnt that gradually change the velocity.x from 20 to 10 instead of to 30?

Ive been trying to do velocity += instead of = is i guess what i’m trying to say, its certainly been confusing

It will move towards your speed variable, by accel over time. If your speed is 30 then it will increase from 20 by accel over one second, I find a good number for accel to be four to eight times as much as speed, so that it reaching max speed in 1/4th to 1/8th of a second.

Ohh so for the effect I’m looking for I could essentially constantly set the speed to be an amount higher than what my current velocity is?

And maybe to keep it consistent i can just factor out whatever the move_toward value is somehow from the velocity?

What I’m planning on doing is completely preventing there to be a max overall speed so you can build it up almost indefinitely. w a s and d character movement should still be useful for minor air control, hence why i need the movement speed to be added over whatever velocity I have

Ah then you would modify the line 36 to only add acceleration

horizontal_velocity += input_direction * accel * delta

Most games do have a max speed, even physics has a terminal velocity, but with the template I posted you can also break past the max speed for a short while; that’s why it’s more of a “target speed” than a max speed.