#How to keep velocity while being in air ?

49 messages · Page 1 of 1 (latest)

thorn needle
#

So, i'm currently making a fast fps movement controller (something similar to Quake), and i'm struggling on how to keep the velocity momentum while being in air, after pressing a direction input.
Here's my movement code part :

var input_dir = Input.get_vector("left", "right", "frontward", "backward") 
var direction = (cameraHolder.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if is_on_floor():
        if direction:
            velocity.x = lerp(velocity.x, direction.x * moveSpeed, moveAcceleration * delta)
            velocity.z = lerp(velocity.z, direction.z * moveSpeed, moveAcceleration * delta)
        else:
            velocity.x = lerp(velocity.x, direction.x * moveSpeed, moveDecceleration * delta)
            velocity.z = lerp(velocity.z, direction.z * moveSpeed, moveDecceleration * delta)
elif !is_on_floor():
        if direction or !direction: 
            velocity.x = lerp(velocity.x, direction.x * moveSpeed, airControlForce * delta)
            velocity.z = lerp(velocity.z, direction.z * moveSpeed, airControlForce * delta)
        
    move_and_slide()

So, does anyone know how i can do that ?

#

How to keep velocity while being in air ?

gritty badge
#

maybe use move_toward() instead of lerp()

#

move_toward would be linear acceleration though, so do consider that

#

also you don't need the elif !is_on_floor(), since it's guaranteed if the if is_on_floor() evaluates as false

#

also direction or !direction always returns true as well

thorn needle
#

I know that, it's just that i find it more clear if it's written this way

#

And yeah, you're right, using move_toward allow to keep the velocity momentum while being in air

gritty badge
#

because right now, your direction or !direction means it's always approaching direction

#

if and no diections are pressed, that direction is 0,0

thorn needle
thorn needle
gritty badge
#

then yea, try move_toward() instead of lerp

thorn needle
#

Yeah, i tried it, and it works well
But just, what are the actuals differences between lerp and move_toward ?
Because i thought it was just that lerp was more fluid than move_toward, but it seens that theres's way more than that

gritty badge
#

which creates exponential movement (looks good in most cases)

#

move_toward is exactly that: move_toward linearly

#

issue with lerp: it's percentage based

#

lerp(a, b, t)

#

a is value if t = 0
b is value if t = 1
t is percentage between the two

#

t = 0.5 gives you the exact middle

#

if you reuse the result in a lerp for a, you can visualize the change

#

say we use lerp(0, 1, 0.5)

#

returns 0.5

#

lerp(0.5, 1, 0.5) = 0.75

#

move_towards moves until it hits the value

#

move_toward(0, 100. 10) = 10
move_toward(10, 100. 10) = 20
move_toward(20, 100. 10) = 30
etc.

thorn needle
#

Oh ok
But using move_toward cause an annoying issue in my code, it drain the air control value

gritty badge
#

wdym drain?

thorn needle
#

If the air control value is high, the velocity momentum is ridiculously low
But if the air control value is low, it's like you have to force yourself to move in air, it's not fluid at all

thorn needle
#

Yeah i know, but that not what i want

gritty badge
#

you are using the same airControlForce for both acceleration and decceleration

#

hence why i asked you "are you sure you want direction or !direction?"

#
        if direction:
            velocity.x = lerp(velocity.x, direction.x * moveSpeed, airControlForce * delta)
            velocity.z = lerp(velocity.z, direction.z * moveSpeed, airControlForce * delta)
        else:
            velocity.x = lerp(velocity.x, direction.x * moveSpeed, moveDecceleration * delta)
            velocity.z = lerp(velocity.z, direction.z * moveSpeed, moveDecceleration * delta)```
#

you prob want this or smt

#

maybe replace moveDecceleration with AirDecceleration

#

or like drag, or smt

thorn needle
#

Yeah, you're right, i was currently correcting my stuff actually 😅

#

Here's my modifications :

elif !is_on_floor():
    if direction: 
      velocity.x = lerp(velocity.x, direction.x * moveSpeed, airControlForce * delta)
      velocity.z = lerp(velocity.z, direction.z * moveSpeed, airControlForce * delta)
    else:
      velocity.x = move_toward(velocity.x, direction.x * moveSpeed, delta)
      velocity.z = move_toward(velocity.z, direction.z * moveSpeed, delta)
gritty badge
#

I would personally use different variables to make it more customizable

#

oh yea, also forgot to replace the lerps in mine

thorn needle
#

Just, if i'm not wrong, this variable will have to be related to the moveSpeed variable

gritty badge
#

physics are just however you want them to be in games

thorn needle
#

Yeah, well for a more juicy controller, i think it's better to keep a velocity related to the last current speed, so it's probably better to have a variable for that related to the velocity