Files
gpd-925-en/AI/Assets/Scripts/FSM/PlayerDetectedState.cs
T
david 10f61fd5d9 Added AI Unity project
Added FSM examples
2026-04-20 18:37:00 +02:00

55 lines
1.4 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");
}
else
{
// option 2: player is too far
Debug.Log("Player is too far");
fsm.ChangeState(new IdleState(_owner, _player, 30.0f));
}
}
public void Exit(IFiniteStateMachine fsm)
{
Debug.Log("PlayerDetectedState Exit");
}
}
}
public static class GameValues
{
public static float PlayerDetectionRadius = 10.0f;
public static float EnemyViewAngle = 30.0f;
}