2023-08-08 07:53:35 +00:00
|
|
|
using System;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.AI;
|
|
|
|
|
|
|
|
// ReSharper disable once CheckNamespace
|
|
|
|
namespace BlueWaterProject
|
|
|
|
{
|
|
|
|
public class AiMover : MonoBehaviour
|
|
|
|
{
|
|
|
|
private enum MoveType
|
|
|
|
{
|
|
|
|
NONE, // 미설정
|
|
|
|
FIXED, // 자리를 지키는 AI
|
|
|
|
MOVE // 자리를 움직이는 AI
|
|
|
|
}
|
|
|
|
|
|
|
|
#region Property and variable
|
|
|
|
|
|
|
|
|
|
|
|
[SerializeField] private MoveType moveType;
|
|
|
|
|
|
|
|
private bool isCommanded;
|
|
|
|
private Vector3 movePos;
|
|
|
|
|
|
|
|
private AiController aiController;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Unity built-in function
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
aiController = Utils.GetComponentAndAssert<AiController>(transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void LateUpdate()
|
|
|
|
{
|
|
|
|
aiController.GetAnimator().SetFloat(AiController.SpeedHash, aiController.GetNavMeshAgent().velocity.normalized.magnitude);
|
|
|
|
|
|
|
|
if (isCommanded || aiController.GetIsAttacking())
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-09 07:44:09 +00:00
|
|
|
if (!GetTargetInfo().transform || moveType is MoveType.NONE or MoveType.FIXED) return;
|
2023-08-08 07:53:35 +00:00
|
|
|
|
2023-08-09 07:44:09 +00:00
|
|
|
GetNavMeshAgent().SetDestination(GetTargetInfo().transform.position);
|
2023-08-08 07:53:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Custom function
|
|
|
|
|
2023-08-15 20:36:04 +00:00
|
|
|
public void MoveTarget(Vector3 targetPos)
|
|
|
|
{
|
|
|
|
isCommanded = true;
|
|
|
|
GetNavMeshAgent().SetDestination(targetPos);
|
|
|
|
}
|
2023-08-08 07:53:35 +00:00
|
|
|
public bool GetIsCommanded() => isCommanded;
|
2023-08-15 20:36:04 +00:00
|
|
|
private TargetInfo GetTargetInfo() => aiController.TargetInfo;
|
2023-08-08 07:53:35 +00:00
|
|
|
private NavMeshAgent GetNavMeshAgent() => aiController.GetNavMeshAgent();
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|