49 lines
1.3 KiB
C#
49 lines
1.3 KiB
C#
using UnityEngine;
|
|
|
|
namespace AI.FSM
|
|
{
|
|
public class PlayerDetectedState : IState
|
|
{
|
|
private GameObject _owner;
|
|
private Transform _player;
|
|
private float _radius;
|
|
|
|
public PlayerDetectedState(GameObject owner, Transform player, float radius)
|
|
{
|
|
_owner = owner;
|
|
_player = player;
|
|
_radius = radius;
|
|
}
|
|
|
|
public void Enter(IFiniteStateMachine fsm)
|
|
{
|
|
Debug.Log("PlayerDetectedState Enter");
|
|
}
|
|
|
|
public void Update(IFiniteStateMachine fsm)
|
|
{
|
|
Debug.Log("PlayerDetectedState Update");
|
|
|
|
// distance to player
|
|
float distance = Vector3.Distance(_owner.transform.position, _player.position);
|
|
|
|
if (distance <= _radius)
|
|
{
|
|
// option 1: player is close enough
|
|
Debug.Log("Player is close enough");
|
|
fsm.SetState(new PlayerIsCloseState());
|
|
}
|
|
else
|
|
{
|
|
// option 2: player is too far
|
|
Debug.Log("Player is too far");
|
|
fsm.SetState(new PlayerIsNotCloseState());
|
|
}
|
|
}
|
|
|
|
public void Exit(IFiniteStateMachine fsm)
|
|
{
|
|
Debug.Log("PlayerDetectedState Exit");
|
|
}
|
|
}
|
|
} |