walker.gd 1004 B

12345678910111213141516171819202122232425262728293031323334353637
  1. class_name Walker
  2. extends CharacterBody3D
  3. # How fast the player moves in meters per second.
  4. @export var speed = 14
  5. # The downward acceleration when in the air, in meters per second squared.
  6. @export var fall_acceleration = 75
  7. var target_velocity = Vector3.ZERO
  8. func _physics_process(delta: float) -> void:
  9. var direction = Vector3.ZERO
  10. if Input.is_action_pressed("move_right"):
  11. direction.x += 1
  12. if Input.is_action_pressed("move_left"):
  13. direction.x -= 1
  14. if Input.is_action_pressed("move_back"):
  15. direction.z += 1
  16. if Input.is_action_pressed("move_forward"):
  17. direction.z -= 1
  18. if direction != Vector3.ZERO:
  19. direction = direction.normalized()
  20. # Ground Velocity
  21. target_velocity.x = direction.x * speed
  22. target_velocity.z = direction.z * speed
  23. # Vertical Velocity
  24. if not is_on_floor(): # If in the air, fall towards the floor. Literally gravity
  25. target_velocity.y = target_velocity.y - (fall_acceleration * delta)
  26. # Moving the Character
  27. velocity = target_velocity
  28. move_and_slide()