OldBlueWater/BlueWater/Assets/09.BehaviorTree/Npc/SetUsuallyPointsAi.cs

66 lines
1.8 KiB
C#

using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;
using UnityEngine;
using UnityEngine.AI;
// ReSharper disable once CheckNamespace
namespace BlueWaterProject
{
[TaskCategory("Custom/NpcAction")]
public class SetUsuallyPointsAi : Action
{
public SharedTransformList usuallyPoints;
private NavMeshAgent agent;
private int destPoint;
private Vector3 lastPosition;
private Vector3 localScale;
public override void OnAwake()
{
agent = GetComponent<NavMeshAgent>();
agent.updateRotation = false;
localScale = transform.localScale;
lastPosition = transform.position;
GoToNextPoint();
}
private void GoToNextPoint()
{
if (usuallyPoints.Value.Count == 0)
return;
destPoint = Random.Range(0, usuallyPoints.Value.Count);
agent.destination = usuallyPoints.Value[destPoint].position;
}
public override TaskStatus OnUpdate()
{
// 현재 이동 방향 계산
var moveDirection = transform.position - lastPosition;
if (moveDirection.x < 0)
{
// 왼쪽으로 이동
localScale.x = Mathf.Abs(localScale.x) * -1;
}
else if (moveDirection.x > 0)
{
// 오른쪽으로 이동
localScale.x = Mathf.Abs(localScale.x);
}
// Scale 업데이트
transform.localScale = localScale;
// 마지막 위치 업데이트
lastPosition = transform.position;
if (!agent.pathPending && agent.remainingDistance < 2f)
{
GoToNextPoint();
}
return TaskStatus.Running;
}
}
}