avoid stones when chasing

This commit is contained in:
2026-04-22 17:27:25 +03:00
parent 017fcac3c5
commit 0a637a6160
+19
View File
@@ -94,12 +94,31 @@ func _chase(delta: float) -> void:
target.take_damage(damage_to_player)
if dist > 0.05:
var dir := diff.normalized()
dir = _avoid_rocks(dir)
velocity.x = dir.x * move_speed
velocity.z = dir.z * move_speed
rotation.y = lerp_angle(rotation.y, atan2(dir.x, dir.z), 8.0 * delta)
velocity.y = 0.0
move_and_slide()
const AVOID_RADIUS := 1.6
const AVOID_STRENGTH := 2.2
func _avoid_rocks(desired: Vector3) -> Vector3:
var push := Vector3.ZERO
for rock in get_tree().get_nodes_in_group("rocks"):
if not is_instance_valid(rock):
continue
var away := global_position - (rock as Node3D).global_position
away.y = 0.0
var d := away.length()
if d < AVOID_RADIUS and d > 0.01:
push += away.normalized() * (1.0 - d / AVOID_RADIUS)
if push.length() < 0.01:
return desired
var steered := desired + push * AVOID_STRENGTH
return steered.normalized() if steered.length() > 0.01 else desired
func _fly(delta: float) -> void:
var speed_now := Vector2(fly_vel.x, fly_vel.z).length()
velocity = fly_vel