walker.gd 804 B

123456789101112131415161718192021222324252627
  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. var ground_velocity: Vector2 = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
  11. # Ground Velocity
  12. target_velocity.x = ground_velocity.x * speed
  13. target_velocity.z = ground_velocity.y * speed
  14. # Vertical Velocity
  15. if not is_on_floor(): # If in the air, fall towards the floor. Literally gravity
  16. target_velocity.y = target_velocity.y - (fall_acceleration * delta)
  17. # Moving the Character
  18. velocity = target_velocity
  19. move_and_slide()