navmesh.gd 1.1 KB

123456789101112131415161718192021222324252627282930
  1. extends Node3D
  2. const Character = preload("res://character.gd")
  3. @onready var _camera := $CameraBase/Camera3D as Camera3D
  4. @onready var _robot := $RobotBase as Character
  5. var _cam_rotation := 0.0
  6. func _unhandled_input(event: InputEvent):
  7. if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
  8. # Get closest point on navmesh for the current mouse cursor position.
  9. var mouse_cursor_position = event.position
  10. var camera_ray_length := 1000.0
  11. var camera_ray_start := _camera.project_ray_origin(mouse_cursor_position)
  12. var camera_ray_end := camera_ray_start + _camera.project_ray_normal(mouse_cursor_position) * camera_ray_length
  13. var closest_point_on_navmesh := NavigationServer3D.map_get_closest_point_to_segment(
  14. get_world_3d().navigation_map,
  15. camera_ray_start,
  16. camera_ray_end
  17. )
  18. _robot.set_target_position(closest_point_on_navmesh)
  19. elif event is InputEventMouseMotion:
  20. if event.button_mask & (MOUSE_BUTTON_MASK_MIDDLE + MOUSE_BUTTON_MASK_RIGHT):
  21. _cam_rotation += event.relative.x * 0.005
  22. $CameraBase.set_rotation(Vector3.UP * _cam_rotation)