walker.gd 936 B

1234567891011121314151617181920212223242526272829303132
  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. # Vertical impulse applied to the character upon jumping in meters per second.
  8. @export var jump_impulse = 20
  9. var target_velocity = Vector3.ZERO
  10. func _physics_process(delta: float) -> void:
  11. var direction = Vector3.ZERO
  12. var ground_velocity: Vector2 = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
  13. # Ground Velocity
  14. target_velocity.x = ground_velocity.x * speed
  15. target_velocity.z = ground_velocity.y * speed
  16. # Vertical Velocity
  17. if is_on_floor():
  18. if Input.is_action_just_pressed("jump"):
  19. target_velocity.y = jump_impulse
  20. else:
  21. target_velocity.y = target_velocity.y - (fall_acceleration * delta)
  22. # Moving the Character
  23. velocity = target_velocity
  24. move_and_slide()