30 lines
821 B
GDScript3
Raw Normal View History

2025-01-02 12:09:30 +01:00
extends CharacterBody3D
const SPEED = 1.0
const ACCEL = 4.5
@onready var nav: NavigationAgent3D = $NavigationAgent3D
@export var target: CharacterBody3D = null
2025-01-04 23:07:42 +01:00
var last_direction = Vector3.ZERO
2025-01-02 12:09:30 +01:00
2025-01-04 23:07:42 +01:00
func set_target(t: CharacterBody3D):
target = t
2025-01-02 12:09:30 +01:00
func _physics_process(delta: float) -> void:
if not target:
return
nav.set_target_position(target.global_position)
var current_location = global_transform.origin
var next_location = nav.get_next_path_position()
2025-01-02 14:05:21 +01:00
var dist = next_location - current_location
var new_velocity = dist.normalized() * SPEED
2025-01-04 23:07:42 +01:00
if snapped(new_velocity.x, 1):
last_direction = Vector3(-sign(new_velocity.x), 1, 1)
var t: Material = $MeshInstance3D.material_override
t.uv1_scale = last_direction
2025-01-02 14:05:21 +01:00
if dist.length() > 0.5:
2025-01-02 12:09:30 +01:00
velocity = new_velocity
move_and_slide()