Added AI Unity project
Added FSM examples
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
using DefaultNamespace;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AI.FSM
|
||||
{
|
||||
public class IdleState : IState
|
||||
{
|
||||
private GameObject _owner;
|
||||
private Transform _player;
|
||||
private float _viewAngle;
|
||||
|
||||
public IdleState(GameObject owner, Transform player, float viewAngle)
|
||||
{
|
||||
_owner = owner;
|
||||
_player = player;
|
||||
_viewAngle = viewAngle;
|
||||
}
|
||||
|
||||
public void Enter(IFiniteStateMachine fsm)
|
||||
{
|
||||
Debug.Log("IdleState Enter");
|
||||
}
|
||||
|
||||
public void Update(IFiniteStateMachine fsm)
|
||||
{
|
||||
Debug.Log("IdleState Update");
|
||||
|
||||
// view angle
|
||||
float angle = Vector3.Angle(_owner.transform.forward, _player.position - _owner.transform.position);
|
||||
if (angle > _viewAngle) return;
|
||||
|
||||
// raycast
|
||||
RaycastHit hit;
|
||||
if (!Physics.Raycast(_owner.transform.position, _player.position - _owner.transform.position, out hit, 100f)) return;
|
||||
// if (hit.collider.transform != _player) return;
|
||||
PlayerController playerController = hit.collider.GetComponent<PlayerController>();
|
||||
if (!playerController) return;
|
||||
|
||||
fsm.ChangeState(new PlayerDetectedState(_owner, _player, 10.0f));
|
||||
}
|
||||
|
||||
public void Exit(IFiniteStateMachine fsm)
|
||||
{
|
||||
Debug.Log("IdleState Exit");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user