TinyAdventure/ai/tasks/look_at_location.gd

43 lines
1.5 KiB
GDScript
Raw Normal View History

@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)
func _generate_name() -> String:
return "Look at Target " + LimboUtility.decorate_var(target_location_var)
func _tick(delta: float) -> Status:
target_position = blackboard.get_var(target_location_var)
update_look_direction(delta)
if absf(_target_look_angle - _current_look_angle) < deg_to_rad(5):
return SUCCESS
return RUNNING
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)
_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)