0 votes

I used this tutorial to learn how to implement FPS movement and then used this tutorial to implement acceleration and friction. Before I added acceleration the player was able to stand on ramps just fine, but now after implementing acceleration the player starts sliding of the ramp when he stops how do I fix this?

physics process function:

velocity.y += gravity * delta
var desired_velocity = get_input() 

if desired_velocity.x != 0:
    velocity.x = lerp(velocity.x, desired_velocity.x * max_speed, acceleration)
    velocity.x = lerp(velocity.x, 0, friction)

if desired_velocity.z != 0:
    velocity.z = lerp(velocity.z, desired_velocity.z * max_speed, acceleration)
    velocity.z = lerp(velocity.z, 0, friction)

velocity = move_and_slide(velocity, Vector3.UP, true)
if jump and is_on_floor():
    velocity.y = jump_speed
in Engine by (17 points)
edited by

While I don't know if this is related to your problem or not, I'd be really leery of those != 0 checks. Those could be failing for you at times due to floating point errors. One way to improve that would be to change those code blocks to be something like:

if !is_equal_approx(desired_velocity.x, 0)

See details here:


1 Answer

0 votes

I would recommend checking two things:

1) Your first component (velocity.y += gravity * delta) will always set a downward velocity, even when the character is rested on the floor. When your character is on the floor it will immediately collide and if this happens on a slope the call to moveandslide may try to slide down the slope. So, it could be a good idea to either only apply gravity when your character is not on the ground, or to explicitly set the y component of velocity to 0 when your character is on the ground (except when jumping). You can determine when your character is on the ground with isonfloor method. Docs here

2) Alternatively, check out moveandslidewithsnap - docs here. This tries to make your character stay on a surface. That might solve your problem, but you need to remember to not use that when the character is jumping or falling.

by (490 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.