Struggling with jumping function in platformer
Posted: Sat May 18, 2024 1:40 am
I decided to try to make my own platformer without using any libraries. I'm doing this project so that I won't be too dependent on libraries to get the work done and I want to genuinely improve my coding skills. One problem I am facing is with jumping and checking in the character is grounded. The thing that has been confusing me is that I am able to get the character to land perfectly but not jump, but when I am able to get the character to jump, it clips through the wall. I tried using a grounded function so it stops moving when it lands, but I came accross this problem...
for some reason, when I use this function in the collision resolution, the player falls through the entire map, however when I try how I did it normally like this
it lands perfectly even though the grounded function has essentially the exact same conditions as the latter. I'm not sure why this is the case
Code: Select all
function isGrounded(gamer, obst)
for val, obst in ipairs(obst) do
if collide(gamer, obst) and (obst.y > gamer.y) then
return true
end
end
end
function slide(gamer, obst)
if collide(gamer, obst) then
local sepX = math.abs((gamer.x + gamer.w / 2)-(obst.x + obst.w / 2))
local sepY = math.abs((gamer.y + gamer.w / 2)-(obst.y + obst.w / 2))
--collision resolution
if sepX > sepY then
gamer.xVel = 0
else
--relavent section
if isGrounded(gamer, obst) then
gamer.y = obst.y - gamer.w
gamer.yVel = 0
end
end
end
end
Code: Select all
else
--relavent section
if obst.y > gamer.y then
gamer.y = obst.y - gamer.w
gamer.yVel = 0
end