46 lines
1.5 KiB
GDScript
46 lines
1.5 KiB
GDScript
@tool
|
|
extends BTAction
|
|
|
|
@export var target_var: StringName = &"target"
|
|
|
|
var target_node: Node3D = null
|
|
|
|
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)
|
|
|
|
func _generate_name() -> String:
|
|
return "Look at Target " + LimboUtility.decorate_var(target_var)
|
|
|
|
func _tick(delta: float) -> Status:
|
|
target_node = blackboard.get_var(target_var)
|
|
|
|
if not is_instance_valid(target_node):
|
|
return FAILURE
|
|
|
|
update_look_direction(delta)
|
|
|
|
if absf(_target_look_angle - _current_look_angle) < deg_to_rad(5):
|
|
return SUCCESS
|
|
|
|
return FAILURE
|
|
|
|
func update_look_direction(delta:float) -> void:
|
|
_target_look_direction = (target_node.global_position - agent.global_position).normalized()
|
|
_current_look_direction = agent.basis.z
|
|
|
|
_current_look_angle = _current_look_direction.signed_angle_to(Vector3.BACK, Vector3.UP)
|
|
_target_look_angle = _target_look_direction.signed_angle_to(Vector3.BACK, Vector3.UP)
|
|
|
|
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
|
|
|
|
var damped_look_angle:float = _look_angle_damper.calc(_current_look_angle, _target_look_angle, delta)
|
|
|
|
_current_look_direction = Vector3(sin(-damped_look_angle), 0, cos(-damped_look_angle))
|
|
agent.basis = Basis.looking_at(-_current_look_direction)
|