Mob.gd 946 B

1234567891011121314151617181920212223242526272829303132333435
  1. extends CharacterBody3D
  2. # Emitted when the player jumped on the mob.
  3. signal squashed
  4. ## Minimum speed of the mob in meters per second.
  5. @export var min_speed = 10
  6. ## Maximum speed of the mob in meters per second.
  7. @export var max_speed = 18
  8. func _physics_process(_delta):
  9. move_and_slide()
  10. func initialize(start_position, player_position):
  11. look_at_from_position(start_position, player_position, Vector3.UP)
  12. rotate_y(randf_range(-PI / 4, PI / 4))
  13. var random_speed = randf_range(min_speed, max_speed)
  14. # We calculate a forward velocity first, which represents the speed.
  15. velocity = Vector3.FORWARD * random_speed
  16. # We then rotate the vector based on the mob's Y rotation to move in the direction it's looking.
  17. velocity = velocity.rotated(Vector3.UP, rotation.y)
  18. $AnimationPlayer.speed_scale = random_speed / min_speed
  19. func squash():
  20. squashed.emit()
  21. queue_free()
  22. func _on_visible_on_screen_notifier_screen_exited():
  23. queue_free()