generated from krampus/template-godot4
63 lines
1.7 KiB
GDScript
63 lines
1.7 KiB
GDScript
extends MechCharacter
|
|
|
|
@export var target: MechCharacter
|
|
|
|
@onready var nav_agent: NavigationAgent3D = $NavigationAgent3D
|
|
|
|
# gdlint: disable=class-definitions-order
|
|
var target_position: Vector3:
|
|
set(value):
|
|
nav_agent.target_position = value
|
|
get:
|
|
return nav_agent.target_position
|
|
# gdlint: enable=class-definitions-order
|
|
|
|
|
|
func _actor_setup() -> void:
|
|
# Wait for the first physics frame so the navserver can sync
|
|
await get_tree().physics_frame
|
|
set_physics_process(true)
|
|
|
|
|
|
func _ready() -> void:
|
|
# Disable physics processing to give the navserver time to sync
|
|
set_physics_process(false)
|
|
# Can't await during _ready, so defer the call with the await
|
|
call_deferred("_actor_setup")
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
var delta_factor: float = delta * GameState.TARGET_FPS
|
|
|
|
if target and not target.in_shadow():
|
|
target_position = target.global_position
|
|
|
|
var relative_target: Vector3 = target_position - global_position
|
|
var angle_to_target: float = atan2(relative_target.x, relative_target.z)
|
|
|
|
var movement: Vector3 = (
|
|
(nav_agent.get_next_path_position() - global_position).normalized()
|
|
if relative_target.length() > 5
|
|
else Vector3.ZERO
|
|
)
|
|
|
|
if movement:
|
|
# Slowly turn mesh towards camera vector when moving on ground
|
|
if is_on_floor():
|
|
var local_target: Vector3 = global_position - target_position
|
|
mesh.global_rotation.y = lerp_angle(
|
|
mesh.global_rotation.y, angle_to_target, delta_factor * TURN_SENSITIVITY
|
|
)
|
|
|
|
var relative_motion_vec: Vector2 = Vector2(-movement.x, -movement.z).rotated(
|
|
mesh.global_rotation.y
|
|
)
|
|
|
|
animation_tree["parameters/walk_space/blend_position"] = lerp(
|
|
animation_tree["parameters/walk_space/blend_position"],
|
|
relative_motion_vec,
|
|
delta_factor * TURN_SENSITIVITY
|
|
)
|
|
|
|
super.physics_process(movement, delta)
|