add navigation to test unit
This commit is contained in:
parent
26747f9618
commit
6f923f6101
@ -72,3 +72,7 @@ reset_camera={
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194332,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
|
||||
[navigation]
|
||||
|
||||
3d/default_cell_height=0.01
|
||||
|
||||
@ -21,3 +21,5 @@ axis = 1
|
||||
texture = ExtResource("2_5725f")
|
||||
|
||||
[node name="NavigationAgent3D" type="NavigationAgent3D" parent="." index="4"]
|
||||
path_desired_distance = 0.5
|
||||
target_desired_distance = 0.5
|
||||
|
||||
@ -1,12 +1,19 @@
|
||||
[gd_scene load_steps=13 format=3 uid="uid://ceru75se7ifkb"]
|
||||
[gd_scene load_steps=14 format=3 uid="uid://ceru75se7ifkb"]
|
||||
|
||||
[ext_resource type="Script" path="res://scripts/main_camera.gd" id="1_xl1ut"]
|
||||
[ext_resource type="PackedScene" uid="uid://cfixshlmwhpmi" path="res://scenes/units/test_unit.tscn" id="2_bk426"]
|
||||
[ext_resource type="Script" path="res://scripts/selector.gd" id="3_522u5"]
|
||||
[ext_resource type="Texture2D" uid="uid://bgbe5l2woe5b2" path="res://assets/textures/gui/selection_box.png" id="4_k36pt"]
|
||||
|
||||
[sub_resource type="NavigationMesh" id="NavigationMesh_6jq54"]
|
||||
vertices = PackedVector3Array(-1.45696, 0.02, -1.07509, -0.706959, 0.02, -1.32509, -0.706959, 0.02, -5.32509, -7.95696, 0.02, -5.32509, -7.95696, 0.02, -0.575085, -1.45696, 0.02, -0.575085, -0.206959, 0.02, -1.07509, 12.543, 0.02, -5.32509, -0.206959, 0.02, -0.325085, -0.706959, 0.02, -0.0750852, -0.706959, 0.02, 5.42491, 12.543, 0.02, 5.42491, -7.95696, 0.02, 5.42491)
|
||||
polygons = [PackedInt32Array(1, 0, 2), PackedInt32Array(2, 0, 3), PackedInt32Array(3, 0, 4), PackedInt32Array(0, 5, 4), PackedInt32Array(1, 2, 6), PackedInt32Array(6, 2, 7), PackedInt32Array(9, 8, 10), PackedInt32Array(10, 8, 11), PackedInt32Array(11, 8, 7), PackedInt32Array(8, 6, 7), PackedInt32Array(9, 10, 5), PackedInt32Array(5, 10, 4), PackedInt32Array(4, 10, 12)]
|
||||
cell_height = 0.01
|
||||
agent_height = 0.5
|
||||
agent_radius = 0.25
|
||||
|
||||
[sub_resource type="PlaneMesh" id="PlaneMesh_qt7ar"]
|
||||
size = Vector2(4, 4)
|
||||
size = Vector2(20.97, 11.275)
|
||||
|
||||
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_21sta"]
|
||||
albedo_color = Color(0.114897, 0.114897, 0.114897, 1)
|
||||
@ -45,17 +52,27 @@ points = PackedVector3Array(-0.0778407, 1.54508, 4.75528, -31.3231, -5, -72.1516
|
||||
|
||||
[node name="World" type="Node3D"]
|
||||
|
||||
[node name="MainCamera" type="Camera3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 0.819152, 0.573576, 0, -0.573576, 0.819152, 0, 1, 2)
|
||||
current = true
|
||||
far = 200.0
|
||||
script = ExtResource("1_xl1ut")
|
||||
|
||||
[node name="Static" type="Node" parent="."]
|
||||
|
||||
[node name="Meshes" type="Node" parent="Static"]
|
||||
[node name="NavigationRegion3D" type="NavigationRegion3D" parent="Static"]
|
||||
navigation_mesh = SubResource("NavigationMesh_6jq54")
|
||||
|
||||
[node name="PlaneMesh" type="MeshInstance3D" parent="Static/Meshes"]
|
||||
[node name="PlaneMesh" type="MeshInstance3D" parent="Static/NavigationRegion3D"]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 2.27804, 0, 0.0624147)
|
||||
mesh = SubResource("PlaneMesh_qt7ar")
|
||||
skeleton = NodePath("")
|
||||
surface_material_override/0 = SubResource("StandardMaterial3D_21sta")
|
||||
|
||||
[node name="BoxMesh" type="MeshInstance3D" parent="Static/Meshes"]
|
||||
[node name="BoxMesh" type="MeshInstance3D" parent="Static/NavigationRegion3D"]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.731028, 0.25, -0.696817)
|
||||
mesh = SubResource("BoxMesh_w7g3k")
|
||||
skeleton = NodePath("")
|
||||
|
||||
[node name="WorldEnvironment" type="WorldEnvironment" parent="Static"]
|
||||
environment = SubResource("Environment_nnr3k")
|
||||
@ -64,12 +81,6 @@ environment = SubResource("Environment_nnr3k")
|
||||
transform = Transform3D(0.707107, -0.5, 0.5, 0, 0.707107, 0.707107, -0.707107, -0.5, 0.5, 0, 0, 0)
|
||||
shadow_enabled = true
|
||||
|
||||
[node name="MainCamera" type="Camera3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 0.819152, 0.573576, 0, -0.573576, 0.819152, 0, 1, 2)
|
||||
current = true
|
||||
far = 200.0
|
||||
script = ExtResource("1_xl1ut")
|
||||
|
||||
[node name="Units" type="Node" parent="."]
|
||||
|
||||
[node name="TestUnit" parent="Units" instance=ExtResource("2_bk426")]
|
||||
|
||||
@ -2,14 +2,54 @@ extends CharacterBody3D
|
||||
class_name TestUnit
|
||||
|
||||
var selected: bool = false
|
||||
var ground_plane: Plane = Plane(Vector3.UP, 0)
|
||||
|
||||
@onready var selection_sprite: Sprite3D = $SelectionSprite
|
||||
@onready var nav_agent: NavigationAgent3D = $NavigationAgent3D
|
||||
@onready var camera: Camera3D = get_viewport().get_camera_3d()
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
set_selected(false)
|
||||
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
_navigate()
|
||||
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if event is InputEventMouseButton:
|
||||
var button_event := event as InputEventMouseButton
|
||||
if button_event.pressed:
|
||||
_set_target_click(button_event.position)
|
||||
|
||||
|
||||
func set_selected(on: bool) -> void:
|
||||
selected = on
|
||||
selection_sprite.visible = selected
|
||||
|
||||
|
||||
func _set_target_click(mouse_pos: Vector2) -> void:
|
||||
var click_position := _click_raycast(mouse_pos)
|
||||
print(click_position)
|
||||
if click_position == null:
|
||||
return
|
||||
|
||||
DebugDraw.marker(click_position)
|
||||
nav_agent.set_target_position(click_position)
|
||||
|
||||
|
||||
func _click_raycast(mouse_pos: Vector2) -> Vector3:
|
||||
var from := camera.position
|
||||
var to := camera.project_ray_normal(mouse_pos)
|
||||
return ground_plane.intersects_ray(from, to)
|
||||
|
||||
|
||||
func _navigate() -> void:
|
||||
if nav_agent.is_navigation_finished():
|
||||
return
|
||||
|
||||
var target_pos := nav_agent.get_next_path_position()
|
||||
DebugDraw.vector(position, target_pos)
|
||||
DebugDraw.marker(nav_agent.get_final_position())
|
||||
# position = target_pos
|
||||
|
||||
Loading…
Reference in New Issue
Block a user