Advertisement
Guest User

2.5d character controller

a guest
Jan 22nd, 2019
2,413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3.  
  4. const MOVE_SPEED = 5
  5. const JUMP_FORCE = 12
  6. const GRAVITY = 9.8
  7. const MAX_FALL_SPEED = 30
  8. var y_velo = 0
  9. var facing_right = false
  10.  
  11. onready var anim_player = $Graphics/AnimationPlayer
  12.  
  13. func _physics_process(delta):
  14. var move_dir = 0
  15. if Input.is_action_pressed("move_right"):
  16. move_dir += 1
  17. if Input.is_action_pressed("move_left"):
  18. move_dir -= 1
  19.  
  20. move_and_slide(Vector3(move_dir * MOVE_SPEED, y_velo, 0), Vector3(0,1,0))
  21.  
  22. var just_jumped = false
  23. var grounded = is_on_floor()
  24. y_velo -= GRAVITY * delta
  25. if y_velo < -MAX_FALL_SPEED:
  26. y_velo = -MAX_FALL_SPEED
  27. if grounded:
  28. y_velo = -0.1
  29. if Input.is_action_pressed("jump"):
  30. y_velo = JUMP_FORCE
  31. just_jumped = true
  32.  
  33. if move_dir < 0 and facing_right:
  34. flip()
  35. if move_dir > 0 and !facing_right:
  36. flip()
  37.  
  38. if just_jumped:
  39. play_anim("jump")
  40. elif grounded:
  41. if move_dir == 0:
  42. play_anim("idle")
  43. else:
  44. play_anim("walk")
  45.  
  46. func flip():
  47. $Graphics.rotation_degrees.y *= -1
  48. facing_right = !facing_right
  49.  
  50. func play_anim(anim):
  51. if anim_player.current_animation == anim:
  52. return
  53. anim_player.play(anim)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement