Added experimental version of unit movement.

This commit is contained in:
Nekojimi 2025-04-21 13:03:19 +01:00
parent c852526648
commit 430abcb294
7 changed files with 207 additions and 94 deletions

View File

@ -1,30 +1 @@
extends GridMap
var timer: float = 0
func _process(delta: float) -> void:
timer += delta
if timer > 1:
timer = 0
test_path()
if NavigationServer3D.get_maps().is_empty():
return
func test_path() -> void:
if NavigationServer3D.get_maps().is_empty():
return
var map_rid: RID = NavigationServer3D.get_maps()[0]
for region_rid in NavigationServer3D.map_get_regions(map_rid):
NavigationServer3D.region_set_travel_cost(region_rid, randf_range(0, 10))
var a: Vector3 = Vector3(1, 1, 1)
var b: Vector3 = Vector3(20,1,20)
var path: PackedVector3Array = NavigationServer3D.map_get_path(map_rid, a, b, true, 1)
DebugDraw3D.draw_sphere(a, 0.5, Color.GREEN, 1)
DebugDraw3D.draw_sphere(b, 0.5, Color.RED, 1)
DebugDraw3D.draw_line_path(path, Color.YELLOW, 1)

File diff suppressed because one or more lines are too long

48
objects/unit.tscn Normal file
View File

@ -0,0 +1,48 @@
[gd_scene load_steps=7 format=3 uid="uid://1gcj3gixy6hs"]
[ext_resource type="Script" uid="uid://f0j7u0so2ug5" path="res://scripts/unit.gd" id="1_dberb"]
[ext_resource type="Texture2D" uid="uid://3javrn230ddq" path="res://assets/images/enemy.png" id="2_2pk7s"]
[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_a0tk4"]
friction = 0.25
bounce = 0.2
[sub_resource type="SphereShape3D" id="SphereShape3D_a202f"]
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_jka67"]
transparency = 2
alpha_scissor_threshold = 0.5
alpha_antialiasing_mode = 0
albedo_texture = ExtResource("2_2pk7s")
texture_filter = 0
billboard_mode = 1
[sub_resource type="QuadMesh" id="QuadMesh_i5arm"]
material = SubResource("StandardMaterial3D_jka67")
[node name="Enemy" type="RigidBody3D"]
axis_lock_angular_x = true
axis_lock_angular_y = true
axis_lock_angular_z = true
physics_material_override = SubResource("PhysicsMaterial_a0tk4")
script = ExtResource("1_dberb")
movement_force = 5.0
max_speed = 5.0
[node name="NavigationAgent3D" type="NavigationAgent3D" parent="."]
path_desired_distance = 0.5
target_desired_distance = 0.5
path_max_distance = 1.01
avoidance_enabled = true
max_speed = 5.0
debug_enabled = true
debug_path_custom_color = Color(1, 0, 0.0808306, 1)
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
shape = SubResource("SphereShape3D_a202f")
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
mesh = SubResource("QuadMesh_i5arm")
[node name="ShapeCast3D" type="ShapeCast3D" parent="."]
shape = SubResource("SphereShape3D_a202f")

View File

@ -8,6 +8,19 @@
config_version=5
[addons]
resources_spreadsheet_view/array_color_tint=100.0
resources_spreadsheet_view/color_rows=true
resources_spreadsheet_view/array_min_width=128.0
resources_spreadsheet_view/resource_preview_size=32.0
resources_spreadsheet_view/clip_headers=false
resources_spreadsheet_view/dupe_arrays=true
resources_spreadsheet_view/context_menu_on_leftclick=false
resources_spreadsheet_view/fold_docks=true
resources_spreadsheet_view/resource_cell_label_mode=1
resources_spreadsheet_view/freeze_first_column=true
[application]
config/name="TowerGame3D"
@ -15,6 +28,13 @@ run/main_scene="uid://bwftban1ppo17"
config/features=PackedStringArray("4.4")
config/icon="res://icon.svg"
[debug]
shapes/navigation/enable_edge_connections_xray=false
shapes/navigation/enable_edge_lines_xray=false
shapes/navigation/enable_link_connections_xray=false
shapes/navigation/enable_agent_paths_xray=false
[editor]
version_control/plugin_name="GitPlugin"
@ -22,11 +42,16 @@ version_control/autoload_on_startup=true
[editor_plugins]
enabled=PackedStringArray("res://addons/PathMesh3D/plugin.cfg", "res://addons/git_describe/plugin.cfg")
enabled=PackedStringArray("res://addons/PathMesh3D/plugin.cfg", "res://addons/git_describe/plugin.cfg", "res://addons/resources_spreadsheet_view/plugin.cfg")
[navigation]
3d/default_edge_connection_margin=1.5
3d/default_edge_connection_margin=0.6
3d/default_link_connection_radius=0.5
[physics]
common/physics_interpolation=true
[rendering]

39
scripts/unit.gd Normal file
View File

@ -0,0 +1,39 @@
extends RigidBody3D
class_name Unit
@export var movement_force: float = 100
@export var max_speed: float = 100
var target_velocity: Vector3 = Vector3()
func _ready() -> void:
$NavigationAgent3D.connect("velocity_computed", avoidance_velocity_computed)
func avoidance_velocity_computed(velocity: Vector3) -> void:
if velocity != target_velocity:
DebugDraw3D.draw_line(global_position, global_position + velocity, Color.ORANGE)
#print("Avoidance issued!")
target_velocity = velocity
func _process(delta: float) -> void:
if $NavigationAgent3D.is_target_reached() or $NavigationAgent3D.target_position.is_zero_approx():
$NavigationAgent3D.target_position = NavigationServer3D.map_get_random_point(NavigationServer3D.get_maps()[0], 1, true)
else:
#DebugDraw3D.draw_sphere($NavigationAgent3D.target_position, 0.5, Color.RED)
var next_point: Vector3 = $NavigationAgent3D.get_next_path_position()
DebugDraw3D.draw_sphere(next_point, 0.1, Color.YELLOW)
var direction: Vector3 = (next_point - global_position).normalized()
#DebugDraw3D.draw_line(global_position, global_position + linear_velocity, Color.BLUE)
var target_velocity: Vector3 = direction * max_speed
$NavigationAgent3D.velocity = target_velocity
DebugDraw3D.draw_line(global_position, global_position + target_velocity, Color.MAGENTA)
func _physics_process(delta: float) -> void:
if $ShapeCast3D.is_colliding():
var force_direction: Vector3 = (target_velocity-linear_velocity)
var normal: Vector3 = $ShapeCast3D.get_collision_normal(0)
DebugDraw3D.draw_line(global_position, global_position + normal, Color.DODGER_BLUE)
var force: Vector3 = (force_direction * movement_force).slide(normal)
DebugDraw3D.draw_line(global_position, global_position + force, Color.GREEN)
apply_central_force(force)

1
scripts/unit.gd.uid Normal file
View File

@ -0,0 +1 @@
uid://f0j7u0so2ug5

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=5 format=3 uid="uid://bexxobgiojrfa"]
[gd_scene load_steps=9 format=3 uid="uid://bexxobgiojrfa"]
[sub_resource type="BoxMesh" id="BoxMesh_qra7f"]
size = Vector3(2, 1, 2)
@ -19,6 +19,22 @@ size = Vector2(2, 2)
center_offset = Vector3(0, 0.5, 0)
orientation = 1
[sub_resource type="PrismMesh" id="PrismMesh_qra7f"]
left_to_right = 0.0
size = Vector3(2, 1, 2)
[sub_resource type="ConvexPolygonShape3D" id="ConvexPolygonShape3D_qra7f"]
points = PackedVector3Array(-1, -0.5, -1, -1, 0.5, -1, 1, -0.5, -1, -1, -0.5, 1, -1, 0.5, 1, 1, -0.5, 1)
[sub_resource type="NavigationMesh" id="NavigationMesh_7h0kd"]
vertices = PackedVector3Array(-0.735052, 0.760437, -0.75, -0.735052, 0.760437, 0.75, 0.764948, 0.010437, 0.75, 0.764948, 0.010437, -0.75)
polygons = [PackedInt32Array(3, 2, 0), PackedInt32Array(0, 2, 1)]
agent_radius = 0.2
[sub_resource type="QuadMesh" id="QuadMesh_k6t24"]
size = Vector2(2.2, 2)
orientation = 1
[node name="Tiles" type="Node3D"]
[node name="Cube" type="MeshInstance3D" parent="."]
@ -34,3 +50,19 @@ navigation_mesh = SubResource("NavigationMesh_qra7f")
[node name="MeshInstance3D" type="MeshInstance3D" parent="Cube/NavigationRegion3D"]
mesh = SubResource("QuadMesh_7h0kd")
[node name="Ramp" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.99928, -0.00256312, 0.000191689)
mesh = SubResource("PrismMesh_qra7f")
[node name="StaticBody3D" type="StaticBody3D" parent="Ramp"]
[node name="CollisionShape3D" type="CollisionShape3D" parent="Ramp/StaticBody3D"]
shape = SubResource("ConvexPolygonShape3D_qra7f")
[node name="NavigationRegion3D" type="NavigationRegion3D" parent="Ramp"]
navigation_mesh = SubResource("NavigationMesh_7h0kd")
[node name="MeshInstance3D" type="MeshInstance3D" parent="Ramp/NavigationRegion3D"]
transform = Transform3D(0.895502, 0.445057, 0, -0.445057, 0.895502, 0, 0, 0, 1, 0, 0, 0)
mesh = SubResource("QuadMesh_k6t24")