Enemy avoidance (I finally figured it out <3), fix camera movement bugging out under specific circumstances

This commit is contained in:
toasterpanic 2026-05-16 14:05:49 -04:00
parent c9bb3287de
commit 659ba0f94d
5 changed files with 106 additions and 62 deletions

50
scenes/dreamer.tscn Normal file
View file

@ -0,0 +1,50 @@
[gd_scene format=3 uid="uid://cwpnnh7v4b5q2"]
[ext_resource type="Script" uid="uid://vi18m3j6f6fa" path="res://scripts/enemies/dreamer.gd" id="1_pa2oe"]
[ext_resource type="PackedScene" uid="uid://cnii80nh1mxr2" path="res://scenes/living/dreamer_body.tscn" id="2_43va2"]
[ext_resource type="Texture2D" uid="uid://b6krbvbco0jt6" path="res://textures/shadow.png" id="3_n5vsr"]
[sub_resource type="BoxShape3D" id="BoxShape3D_trtic"]
size = Vector3(0.65, 1, 0.65)
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_yqjtg"]
radius = 0.15
height = 1.0
[node name="Dreamer" type="CharacterBody3D" unique_id=272925354]
collision_layer = 2
collision_mask = 2
script = ExtResource("1_pa2oe")
[node name="HitCollision" type="Area3D" parent="." unique_id=258957044]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.1500001, 0)
collision_layer = 0
[node name="Shape" type="CollisionShape3D" parent="HitCollision" unique_id=681211050]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.5)
shape = SubResource("BoxShape3D_trtic")
[node name="DreamerBody" parent="." unique_id=723408850 instance=ExtResource("2_43va2")]
[node name="Shadow" type="Sprite3D" parent="." unique_id=66990281]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -2, 0)
modulate = Color(1, 1, 1, 0.5764706)
pixel_size = 0.03
axis = 1
texture_filter = 0
texture = ExtResource("3_n5vsr")
[node name="Collision" type="CollisionShape3D" parent="." unique_id=1709995346]
shape = SubResource("CapsuleShape3D_yqjtg")
[node name="Floorcast" type="RayCast3D" parent="." unique_id=1016475353]
target_position = Vector3(0, -16, 0)
[node name="Navigator" type="NavigationAgent3D" parent="." unique_id=919187616]
path_postprocessing = 2
avoidance_enabled = true
neighbor_distance = 1.5
max_speed = 2.5
debug_enabled = true
[connection signal="velocity_computed" from="Navigator" to="." method="_on_navigator_velocity_computed"]

File diff suppressed because one or more lines are too long

View file

@ -34,6 +34,10 @@ var state_timer := 0.0
@onready var navigation_agent: NavigationAgent3D = $Navigator @onready var navigation_agent: NavigationAgent3D = $Navigator
func _on_navigator_velocity_computed(safe_velocity: Vector3) -> void:
velocity = safe_velocity
move_and_slide()
func hit_me(): func hit_me():
iframes = 0.5 iframes = 0.5
health -= 10 health -= 10
@ -68,8 +72,10 @@ func _process(delta: float) -> void:
knockback_time -= delta knockback_time -= delta
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
var intended_velocity = velocity
if not is_on_floor(): if not is_on_floor():
velocity += get_gravity() * delta intended_velocity += get_gravity() * delta
var viewport_half = (get_viewport().get_visible_rect().size / 2.0) var viewport_half = (get_viewport().get_visible_rect().size / 2.0)
var player_position = Vector2.ZERO #camera.unproject_position(global_transform.origin) var player_position = Vector2.ZERO #camera.unproject_position(global_transform.origin)
@ -109,7 +115,7 @@ func _physics_process(delta: float) -> void:
$DreamerBody/Body/ArmPivot.rotation.z = 0 $DreamerBody/Body/ArmPivot.rotation.z = 0
if false: if false:
velocity.y = JUMP_VELOCITY intended_velocity.y = JUMP_VELOCITY
$DreamerBody/Animator.set("parameters/jump/request", AnimationNodeOneShot.ONE_SHOT_REQUEST_FIRE) $DreamerBody/Animator.set("parameters/jump/request", AnimationNodeOneShot.ONE_SHOT_REQUEST_FIRE)
var direction = Vector3.ZERO var direction = Vector3.ZERO
@ -179,17 +185,26 @@ func _physics_process(delta: float) -> void:
face_left = true face_left = true
if direction and (knockback_time <= 0): if direction and (knockback_time <= 0):
velocity.x = direction.x * SPEED intended_velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED intended_velocity.z = direction.z * SPEED
$DreamerBody/Animator.set("parameters/walk/blend_amount", abs(direction.length())) $DreamerBody/Animator.set("parameters/walk/blend_amount", abs(direction.length()))
navigation_agent.avoidance_enabled = true
navigation_agent.set_velocity(intended_velocity)
else: else:
velocity.x = move_toward(velocity.x, 0, delta * 4) intended_velocity.x = move_toward(intended_velocity.x, 0, delta * 4)
velocity.z = move_toward(velocity.z, 0, delta * 4) intended_velocity.z = move_toward(intended_velocity.z, 0, delta * 4)
$DreamerBody/Animator.set("parameters/walk/blend_amount", 0) $DreamerBody/Animator.set("parameters/walk/blend_amount", 0)
navigation_agent.avoidance_enabled = false
velocity = intended_velocity
move_and_slide()
if direction and is_on_floor(): if direction and is_on_floor():
walk_animation_time += delta * 25 walk_animation_time += delta * 25
@ -212,9 +227,6 @@ func _physics_process(delta: float) -> void:
else: else:
$DreamerBody/Animator.set("parameters/fall/blend_amount", 0) $DreamerBody/Animator.set("parameters/fall/blend_amount", 0)
$DreamerBody/Animator.set("parameters/leap/blend_amount", 1) $DreamerBody/Animator.set("parameters/leap/blend_amount", 1)
move_and_slide()
$Shadow.global_position.y = $Floorcast.get_collision_point().y + 0.01 $Shadow.global_position.y = $Floorcast.get_collision_point().y + 0.01

View file

@ -74,7 +74,7 @@ func _process(delta: float) -> void:
else: else:
$DreamerBody/Animator.set("parameters/hit/blend_amount", 0.0) $DreamerBody/Animator.set("parameters/hit/blend_amount", 0.0)
func _physics_process(delta: float) -> void: #func _physics_process(delta: float) -> void:
$DreamerBody.look_at(game.get_node("PlayerCamera").global_position) $DreamerBody.look_at(game.get_node("PlayerCamera").global_position)
$DreamerBody.rotation.x = 0 $DreamerBody.rotation.x = 0
face_rotation = $DreamerBody.rotation.y face_rotation = $DreamerBody.rotation.y
@ -215,7 +215,7 @@ func _physics_process(delta: float) -> void:
var knockback_pos = global_position var knockback_pos = global_position
knockback_pos.y = 0 knockback_pos.y = 0
var knockback_body_pos = body.global_position var knockback_body_pos = $HitCollision/Shape.global_position
knockback_body_pos.y = 0 knockback_body_pos.y = 0
body.position.y += 0.2 body.position.y += 0.2
@ -277,3 +277,5 @@ func _physics_process(delta: float) -> void:
$Shadow.global_position.y = $Floorcast.get_collision_point().y + 0.01 $Shadow.global_position.y = $Floorcast.get_collision_point().y + 0.01
$Label3D.text = str(velocity.length())

View file

@ -9,8 +9,9 @@ func _ready() -> void:
pass pass
func _process(delta: float) -> void: func _process(delta: float) -> void:
var goal_position = player.get_node("Collision").global_position + offset var goal_position = player.get_node("Collision").global_position + offset
global_position += (goal_position - global_position) / (500 * delta)
global_position = lerp(global_position, goal_position, min(delta * 4, 1.0))
for n in camera_zones.get_children(): for n in camera_zones.get_children():
if player in n.get_overlapping_bodies(): if player in n.get_overlapping_bodies():