Advertisement
chris33556

updated_punch_and_walk_10_10_22

Oct 10th, 2022
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3. var speed = 50
  4. var velocity = Vector3.ZERO
  5. var input_vector = Vector3.ZERO
  6.  
  7. onready var Anim = $AnimatedSprite3D
  8. var acceleration = 1
  9. var friction = 1
  10.  
  11. enum state {IDLE,WALK,PUNCH,FALL}
  12. var player_state = state.IDLE
  13.  
  14. func get_input():
  15. var dir = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
  16. if dir !=0:
  17. velocity.x = move_toward(velocity.x, dir * speed, acceleration)
  18. else:
  19. velocity.x = move_toward(velocity.x, 0, friction)
  20.  
  21.  
  22.  
  23. func update_animation():
  24.  
  25. if velocity.x > 0:
  26. Anim.flip_h = false
  27. scale.x = 29
  28. if velocity.x < 0:
  29. Anim.flip_h = false
  30. scale.x = -29
  31. match(player_state):
  32. state.IDLE:
  33. Anim.play("idle")
  34. state.WALK:
  35. Anim.play("walk")
  36.  
  37. state.PUNCH:
  38. Anim.play("punch")
  39. yield($AnimatedSprite3D, "animation_finished")
  40. player_state = state.IDLE
  41.  
  42.  
  43.  
  44. func _physics_process(delta):
  45.  
  46. if player_state != state.PUNCH:
  47. get_input()
  48.  
  49.  
  50. if velocity.x == 0:
  51. velocity.x = 0
  52. player_state = state.IDLE
  53.  
  54. elif velocity.x != 0:
  55. player_state = state.WALK
  56.  
  57. if velocity.x == 0:
  58. if Input.is_action_just_pressed("punch"):
  59. player_state = state.PUNCH
  60.  
  61.  
  62. if player_state == state.PUNCH and player_state != state.WALK:
  63. velocity.x = 0
  64.  
  65.  
  66.  
  67. velocity = move_and_slide(velocity)
  68. update_animation()
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement