Ok, so first of all, if you're new, it's much better to start by following some youtube tutorials than by just copying code. It'll be slower, but the tutorial will explain exactly what each line means and why it's there.
The two move_and_slide
commands probably aren't an error, but I'd rather do without them.
In your physics_process
function, you first calculate the direction the player is moving and save it as X coordinate of your 'motion' variable. That's perfectly fine.
Next, you calculate gravity, but for some reason, instead of saving it as the Y coordinate of your motion variable, you save it as a separate one and execute it instantly with move_and_slide_with_snap()
.
You later do some checks for dash etc. and replace the regular movement with the dash one for a certain period of time.
And finally, the horizontal movement is executed with move_and_slide()
.
What I would change is instead of saving the gravity to the 'velocity' variable, just save it to the motion variable again and get rid of the first move_and_slide_with_snap
. Then later, in your dash code, you'll be able to simply set motion.y to 0, meaning you'll remove whatever influence gravity had on player's movement.
Also, you'll need to replace motion = motion * MOTION_SPEED
with motion.x = motion.x * SPEED
, so that the MOTION_SPEED variable only affects the horizontal speed, not vertical.
As for your question, when you're making a 2D platformer, move_and_slide()
requires second parameter - an UP vector. Functions like is_on_floor()
need to somehow check which direction the floor is. Maybe you're making a ninja game when the player's running upwards, or maybe the gravity is inverted, the game doesn't know that unless you tell it.
So when you use move_and_slide()
you need two parameters: first is the motion vector, which tells it which direction to move the object it's controlling (here: the player). The second tells it which way is UP, so it can perform correct floor and ceiling checks for those functions I mentioned.
So in your case, the correct way to use it would be move_and_slide(motion, Vector2.UP)
or move_and_slide(motion, Vector2(0, -1))
or move_and_slide(motion, Vector2.UP)
. The latter works, since Godot knows it's a commonly used value and they just have it preset. You actually did use that unknowingly in move_and_slide_with_snap
in your code already.
Hope that clears it up a bit. Now go and follow some tutorial so that you actually know what you're doing next time :)