Ultraworked with [Sisyphus](https://github.com/code-yeonggu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
77 lines
2.0 KiB
C#
77 lines
2.0 KiB
C#
using System;
|
|
using Unity.Behavior;
|
|
using UnityEngine;
|
|
using Action = Unity.Behavior.Action;
|
|
using Unity.Properties;
|
|
|
|
[Serializable, GeneratePropertyBag]
|
|
[NodeDescription(name: "ChaseTarget", story: "타겟 추적", category: "Action", id: "0889fbb015b8bf414ef569af08bb6868")]
|
|
public partial class ChaseTargetAction : Action
|
|
{
|
|
|
|
[SerializeReference]
|
|
public BlackboardVariable<GameObject> Target;
|
|
|
|
[SerializeReference]
|
|
public BlackboardVariable<float> Speed = new BlackboardVariable<float>(0f);
|
|
|
|
[SerializeReference]
|
|
public BlackboardVariable<float> StopDistance = new BlackboardVariable<float>(2f);
|
|
|
|
private UnityEngine.AI.NavMeshAgent agent;
|
|
|
|
protected override Status OnStart()
|
|
{
|
|
if (Target.Value == null)
|
|
{
|
|
return Status.Failure;
|
|
}
|
|
|
|
agent = GameObject.GetComponent<UnityEngine.AI.NavMeshAgent>();
|
|
if (agent == null)
|
|
{
|
|
Debug.LogWarning("[ChaseTarget] NavMeshAgent not found");
|
|
return Status.Failure;
|
|
}
|
|
|
|
// Speed가 0 이하면 NavMeshAgent의 기존 speed 유지 (EnemyData에서 설정한 값)
|
|
if (Speed.Value > 0f)
|
|
{
|
|
agent.speed = Speed.Value;
|
|
}
|
|
agent.stoppingDistance = StopDistance.Value;
|
|
agent.isStopped = false;
|
|
|
|
return Status.Running;
|
|
}
|
|
|
|
protected override Status OnUpdate()
|
|
{
|
|
if (Target.Value == null)
|
|
{
|
|
return Status.Failure;
|
|
}
|
|
|
|
// 이미 사거리 내에 있으면 성공
|
|
float distance = Vector3.Distance(GameObject.transform.position, Target.Value.transform.position);
|
|
if (distance <= StopDistance.Value)
|
|
{
|
|
agent.isStopped = true;
|
|
return Status.Success;
|
|
}
|
|
|
|
// 타겟 위치로 이동
|
|
agent.SetDestination(Target.Value.transform.position);
|
|
return Status.Running;
|
|
}
|
|
|
|
protected override void OnEnd()
|
|
{
|
|
if (agent != null)
|
|
{
|
|
agent.isStopped = true;
|
|
}
|
|
}
|
|
}
|
|
|