52 lines
1.8 KiB
GDScript
52 lines
1.8 KiB
GDScript
@tool
|
|
extends BTAction
|
|
|
|
@export var target_location_var: StringName = &"target_location"
|
|
|
|
var target_position: Vector3 = Vector3.ZERO
|
|
|
|
var _current_look_direction:Vector3 = Vector3.BACK
|
|
var _current_look_angle:float = 0
|
|
var _target_look_direction:Vector3 = Vector3.BACK
|
|
var _target_look_angle:float = 0
|
|
var _look_angle_damper:SpringDamper = SpringDamper.new(0, 2, 0.06, 0.003)
|
|
var _agent_npc:NonPlayerCharacter = null
|
|
|
|
func _generate_name() -> String:
|
|
return "LookAtTarget(%s)" % LimboUtility.decorate_var(target_location_var)
|
|
|
|
func _tick(delta: float) -> Status:
|
|
_agent_npc = agent as NonPlayerCharacter
|
|
target_position = blackboard.get_var(target_location_var)
|
|
|
|
update_look_direction(delta)
|
|
|
|
if DebugSystem.debug_npc == _agent_npc: DebugDraw3D.draw_arrow(_agent_npc.global_position, target_position, Color.CRIMSON, 0.1, true)
|
|
|
|
if absf(_target_look_angle - _current_look_angle) < deg_to_rad(5):
|
|
return SUCCESS
|
|
|
|
return RUNNING
|
|
|
|
func clamp_current_look_angle() -> void:
|
|
if _target_look_angle - _current_look_angle > PI:
|
|
_current_look_angle = _current_look_angle + PI * 2
|
|
elif _current_look_angle - _target_look_angle > PI:
|
|
_current_look_angle = _current_look_angle - PI * 2
|
|
|
|
func update_look_direction(delta:float) -> void:
|
|
_target_look_direction = (target_position - agent.global_position).normalized()
|
|
_current_look_direction = agent.basis.z
|
|
|
|
_current_look_angle = _current_look_direction.signed_angle_to(Vector3.BACK, Vector3.UP)
|
|
clamp_current_look_angle()
|
|
|
|
_target_look_angle = _target_look_direction.signed_angle_to(Vector3.BACK, Vector3.UP)
|
|
|
|
_current_look_angle = _look_angle_damper.calc(_current_look_angle, _target_look_angle, delta)
|
|
clamp_current_look_angle()
|
|
|
|
_current_look_direction = Vector3(sin(-_current_look_angle), 0, cos(-_current_look_angle))
|
|
|
|
agent.basis = Basis.looking_at(-_current_look_direction)
|