Advertisement
Guest User

Character2.gd

a guest
Oct 12th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends KinematicBody2D
  2.  
  3. var velocity = Vector2.ZERO
  4.  
  5. export var max_run_speed = 350
  6. export var max_jump_speed = -1000
  7. export var max_gravity = 2500
  8.  
  9. var run_speed = max_run_speed
  10. var jump_speed = max_jump_speed
  11. var gravity = max_gravity
  12.  
  13. var jump_possible = true
  14. export var max_coyote_time = 0.075
  15. var coyote_time = max_coyote_time
  16.  
  17.  
  18. func get_input():
  19.     velocity.x = 0
  20.     var right = Input.is_action_pressed("ui_right")
  21.     var left = Input.is_action_pressed("ui_left")
  22.     var jump = Input.is_action_just_pressed("ui_select")
  23.    
  24.     if right:
  25.         $Sprite.flip_h = 1
  26.         velocity.x += run_speed
  27.     if left:
  28.         $Sprite.flip_h = 0
  29.         velocity.x -= run_speed
  30.     if jump_possible and jump:
  31.         velocity.y = jump_speed
  32.         coyote_time = 0
  33.  
  34.  
  35. func _physics_process(delta):
  36.     if coyote_time > 0 and velocity.x != 0:
  37.         coyote_time -= delta
  38.     else:
  39.         coyote_time = 0
  40.         jump_possible = false
  41.     if is_on_floor():
  42.         velocity.y = 0
  43.         coyote_time = max_coyote_time
  44.         jump_possible = true
  45.     elif coyote_time <= 0:
  46.         velocity.y += gravity * delta
  47.     get_input()
  48.     move_and_slide(velocity, Vector2(0, -1))
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement