Advertisement
AJ08Coder

Beat 'em up jump mechanic tutorial

Sep 27th, 2021
1,514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends KinematicBody2D
  2.  
  3. var MAX_SPEED = 100
  4. var ACCELERATION = 500
  5. var JUMP_SPEED = 25
  6. var GRAVITY = 5
  7.  
  8. var motion= Vector2()
  9.  
  10. var z = 0
  11.  
  12. var jumpMultiplyer = 8
  13.  
  14. var Jumping = false
  15.  
  16.  
  17. func _physics_process(delta):
  18.     var axis = get_input_axis()
  19.     print(Jumping)
  20.     if axis == Vector2.ZERO:
  21.         if  Jumping == false:
  22.             $AnimationPlayer.play("Idle")
  23.         apply_friction(ACCELERATION * delta)
  24.     else:
  25.         if  Jumping == false:
  26.             $AnimationPlayer.play("Run")
  27.         apply_movement(axis * ACCELERATION * delta)
  28.        
  29.     if global_position.y < 129.693 and Jumping == false:
  30.         motion.y = 0
  31.         if axis.y > 0:
  32.             apply_movement(axis * ACCELERATION * delta)
  33.        
  34.     if Input.is_action_just_pressed("jump") and Jumping == false:
  35.         $AnimationPlayer.play("Jump")
  36.         Jumping = true
  37.         z += -JUMP_SPEED * jumpMultiplyer
  38.        
  39.     if z > 25 * jumpMultiplyer:
  40.         Jumping = false
  41.         z = 0
  42.         motion.y = 0
  43.        
  44.        
  45.     if Jumping == true:
  46.         z += GRAVITY
  47.         motion.y = z
  48.    
  49.     motion = move_and_slide(motion)
  50.    
  51.    
  52.  
  53. func get_input_axis():
  54.     var axis = Vector2.ZERO
  55.     axis.x = int(Input.is_action_pressed("ui_right")) - int(Input.is_action_pressed("ui_left"))
  56.     axis.y = int(Input.is_action_pressed("ui_down")) - int(Input.is_action_pressed("ui_up"))
  57.    
  58.     if axis.x > 0:
  59.         $Sprite.flip_h = false
  60.     elif axis.x < 0:
  61.         $Sprite.flip_h = true
  62.    
  63.     return axis.normalized()
  64.    
  65. func apply_friction(amount):
  66.     if motion.length() > amount:
  67.         motion -= motion.normalized() * amount
  68.     else:
  69.         motion = Vector2.ZERO
  70.  
  71. func apply_movement(accel):
  72.     motion += accel
  73.     motion = motion.clamped(MAX_SPEED)
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement