What's happening is your player scene is jumping quickly in the direction they're travelling at seemingly random intervals. If you run your scene and go to the Remote view of the scene tree in the editor, you can inspect the values of your various scenes and nodes. Watch your Player's transform when they disappear and it'll read in the hundreds in one or both axes. So the player is still "visible", but they've just catapulted off-screen.
I was able to "fix" this by simply not reassigning velocity to the result of move_and_slide()
in _physics_process()
:
func _physics_process(_delta):
move_and_slide(velocity * speed)
apply_friction()
This stops the player from teleporting, but I'm not sure it will produce the result you want. I assume your apply_friction()
function is meant to bring your player to a sliding stop over time. A couple other methods to do this could be to use lerp() or move_toward()
in a similar way to how you're doing it now:
lerp():
speed = lerp(speed, 0, friction)
move_and_slide(direction * speed)
move_toward():
velocity = velocity.move_toward(Vector2.ZERO, friction)
move_and_slide(velocity)
Both of these methods are very similar, where the lerp()
option is affecting the speed of your character and applying it to a movement direction Vector2, while the move_toward()
method is affecting the whole velocity vector which already has speed factored into it. Additionally, you're already doing the second method with move_toward()
, but you don't need to break it up into individual axes! move_toward()
will also take Vector2s.
I'd look into a couple movement controllers in the asset library and/or movement tutorials to get some ideas on even more ways to move your player, and how to properly use delta in your physics and process functions. Using delta in your movement calculations helps ensure a consistent move speed across varying framerates.
Here's a helpful godot doc going over a few different methods of moving in 2D:
https://docs.godotengine.org/en/stable/tutorials/2d/2d_movement.html
And here's a video from a helpful tutorial series going over the basics of making a basic, 2D top-down RPG:
https://www.youtube.com/watch?v=EQA9MJ5_TxU&list=PL9FzW-m48fn2SlrW0KoLT4n5egNdX-W9a&index=2