Files
gpd-924/AI/Assets/Scripts/FSM/PlayerDetectedState.cs
T
david 19a08085fe add unity project
add example for FSM
2025-04-08 13:03:11 +02:00

49 lines
1.2 KiB
C#

using System;
using UnityEngine;
namespace DefaultNamespace.FSM
{
[Serializable]
public class PlayerDetectedState : IState
{
[SerializeField] private float _radius;
private GameObject _owner;
private PlayerController _playerController;
public PlayerDetectedState(GameObject owner, PlayerController player, float radius)
{
_owner = owner;
_playerController = player;
_radius = radius;
}
public void Enter(IFiniteStateMachine ctx)
{
}
public void Update(IFiniteStateMachine ctx)
{
// get distance to player
float distance = Vector3.Distance(_owner.transform.position, _playerController.transform.position);
// compare to radius
if (distance < _radius)
{
// set to is in range state
Debug.Log("Player is in range");
}
else
{
// set to is out of range state
Debug.Log("Player is out of range");
}
}
public void Exit(IFiniteStateMachine ctx)
{
}
}
}