add unity project
add FSM examples
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace DefaultNamespace.FSM
|
||||
{
|
||||
public class IdleState : IState
|
||||
{
|
||||
private GameObject _owner;
|
||||
private PlayerController _player;
|
||||
|
||||
private float _viewAngle;
|
||||
private float _cosViewAngle;
|
||||
|
||||
public IdleState(GameObject owner, PlayerController player, float viewAngle)
|
||||
{
|
||||
_owner = owner;
|
||||
_player = player;
|
||||
_cosViewAngle = Mathf.Cos(viewAngle * Mathf.Deg2Rad);
|
||||
}
|
||||
|
||||
public void Enter(IFiniteStateMachine ctx)
|
||||
{
|
||||
Debug.Log("IDLE: Enter");
|
||||
}
|
||||
|
||||
public void Update(IFiniteStateMachine ctx)
|
||||
{
|
||||
Debug.Log("IDLE: Update");
|
||||
|
||||
float angle = Vector3.Angle(_owner.transform.forward, _player.transform.position - _owner.transform.position);
|
||||
// if (angle > _viewAngle)
|
||||
// if (cosAngle > _cosViewAngle)
|
||||
|
||||
// player visibility check
|
||||
if (Physics.Raycast(_owner.transform.position, _owner.transform.forward, out RaycastHit hit))
|
||||
{
|
||||
if (hit.transform.GetComponent<PlayerController>() == _player)
|
||||
{
|
||||
ctx.SetState(new ChasePlayerState(_owner, _player));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Exit(IFiniteStateMachine ctx)
|
||||
{
|
||||
Debug.Log("IDLE: Exit");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user