function player.controls(dt)
if isDown("w") and
player.yv < 5 then
player.yv = -player.ms
end
end
function player.physics(dt)
if player.y < groundLevel then
player.yv = player.yv + player.grav * dt
end
end
What you have there is the 'jetpack' effect where the velocity is increased over time.
Ideally you want to change the velocity once - when the player presses the jump button...
then you allow gravity to pull the player down.
However if the player releases the jump button while he is still ascending, you set his vertical velocity to 0.
That's the simplest approach and a good starting point.
function player.controls(dt)
if player.canjump and isDown("w") and
player.yv < 5 then
player.yv = -player.ms
player.canjump = false
end
end
function player.physics(dt)
if player.y < groundLevel then
player.yv = player.yv + player.grav * dt
else
player.canjump = true
end
end
Thank you Robin for the reply It helped a lot! But now if I jump he's just teleports (jumpHeight amount) up, and then down... I would like to make it more smooth... Any suggestions?
As far as I can tell from the code, you would basically need a way to slowly decrease the players velocity. So when the velocity going up after the jump is set, then you need something that every frame would reduce it by the gravity.
Eg.
love.load()
player.yv = 10
gravity = 2
end
love.update(dt)
player.yv = player.yv - gravity
end
Ehm, I can't run the .love or extract the files from it. Are you sure you put your files in a .zip archive, and didn't use some other archive format, like .rar?