quhluh icon

jump mechanic

quhluh | 12/15/23 08:16:51 PM UTC | 0 ⭐ | 1190 👁️ | Never ⏰ | [jump]
Lua |

850 B

|

None

|

0 👍

/

0 👎

-- Define player properties
local player = {
    x = 0,
    y = 0,
    velocityY = 0,
    isJumping = false
}
 
-- Gravity affecting the player
local gravity = 0.5
 
-- Jump strength
local jumpStrength = -10
 
-- Function to handle player jumping
function jump()
    if not player.isJumping then
        player.velocityY = jumpStrength
        player.isJumping = true
    end
end
 
-- Function to update player position
function updatePlayer()
    player.y = player.y + player.velocityY
    player.velocityY = player.velocityY + gravity
 
    -- Check if the player has landed
    if player.y > 0 then
        player.y = 0
        player.isJumping = false
    end
end
 
-- Example usage
jump()  -- Trigger a jump
updatePlayer()  -- Update player position
 
-- Print player position (for testing)
print("Player Y position: " .. player.y)

Comments