Advertisement
chris33556

simple walk/idle script

Sep 30th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3. enum States {IDLE = 1, WALK}
  4. var state = States.WALK
  5. var velocity = Vector3.ZERO
  6. var speed = 55
  7. onready var AnimSprite = $AnimatedSprite3D
  8.  
  9. func _physics_process(delta):
  10. if state == States.IDLE:
  11. idle_loop(delta)
  12. else:
  13. state == States.WALK
  14. walk_loop(delta)
  15.  
  16. print(state)
  17.  
  18.  
  19. func walk_loop(delta):
  20. var input_vector = Vector3.ZERO
  21. input_vector.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
  22. input_vector.z = Input.get_action_strength("move_up") - Input.get_action_strength("move_down")
  23. input_vector = input_vector.normalized()
  24.  
  25.  
  26. if input_vector != Vector3.ZERO:
  27. velocity.x = input_vector.x * speed
  28. velocity.z = input_vector.z * -speed
  29. AnimSprite.play("walk")
  30.  
  31. velocity = move_and_slide(velocity * speed * delta)
  32. return velocity.normalized()
  33.  
  34. func idle_loop(delta):
  35. AnimSprite.play("idle")
  36. velocity.x = 0
  37. velocity.z = 0
  38.  
  39.  
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement