quhluh

jump mechanic

Dec 15th, 2023
1,217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.85 KB | None | 0 0
  1. -- Define player properties
  2. local player = {
  3.     x = 0,
  4.     y = 0,
  5.     velocityY = 0,
  6.     isJumping = false
  7. }
  8.  
  9. -- Gravity affecting the player
  10. local gravity = 0.5
  11.  
  12. -- Jump strength
  13. local jumpStrength = -10
  14.  
  15. -- Function to handle player jumping
  16. function jump()
  17.     if not player.isJumping then
  18.         player.velocityY = jumpStrength
  19.         player.isJumping = true
  20.     end
  21. end
  22.  
  23. -- Function to update player position
  24. function updatePlayer()
  25.     player.y = player.y + player.velocityY
  26.     player.velocityY = player.velocityY + gravity
  27.  
  28.     -- Check if the player has landed
  29.     if player.y > 0 then
  30.         player.y = 0
  31.         player.isJumping = false
  32.     end
  33. end
  34.  
  35. -- Example usage
  36. jump()  -- Trigger a jump
  37. updatePlayer()  -- Update player position
  38.  
  39. -- Print player position (for testing)
  40. print("Player Y position: " .. player.y)
Tags: jump
Advertisement
Add Comment
Please, Sign In to add comment