Unity Platform 15 === 2020.08.25 ###### tags: `platform` ## List 1. [How to make new State](#HS) 2. [Player Detected](#PD) 3. [Update entity and other state](#US) ## Content ### How to make new State<a id="HS"/> [Enermy State logic diagram](https://www.youtube.com/watch?v=GB1Ri_SRtZY&list=PLy78FINcVmjA0zDBhLuLNL1Jo6xNMMq-W&index=15) : 00:25 ~ 1. Create new State class 2. Create State data class 3. Create enermy specific state 4. Declare state on enermy class 5. Set up animatior ### Player Detected<a id="PD"/> Player detects in some range.<br>This project show just liner detect, but use box overlap is better.<br>Use same Logic. **State** ```csharp= public class PlayerDetectedState : State { protected D_PlayerDetectedState stateData; protected bool isPlayerMinAgroRange; protected bool isPlayerMaxAgroRange; public PlayerDetectedState(Entity entity, FiniteStateMachine stateMachine, string animBoolName, D_PlayerDetectedState stateData) : base(entity, stateMachine, animBoolName) { this.stateData = stateData; } public override void Enter() { base.Enter(); entity.SetVelocity(0f); isPlayerMinAgroRange = entity.CheckPlayerInMinAgroRange(); isPlayerMaxAgroRange = entity.CheckPlayerInMaxAgroRange(); } public override void Exit() { base.Exit(); } public override void LogicUpdate() { base.LogicUpdate(); } public override void PhysicsUpdate() { base.PhysicsUpdate(); } } ``` **E1 State** ```csharp= public class E1_PlayerDetectedState : PlayerDetectedState { private E1 enermy; public E1_PlayerDetectedState(Entity entity, FiniteStateMachine stateMachine, string animBoolName, D_PlayerDetectedState stateData, E1 enermy) : base(entity, stateMachine, animBoolName, stateData) { this.enermy = enermy; } public override void Enter() { base.Enter(); } public override void Exit() { base.Exit(); } public override void LogicUpdate() { base.LogicUpdate(); if (!isPlayerMaxAgroRange) { enermy.idleState.SetFlipAfterIdle(false); stateMachine.ChangeState(enermy.idleState); } } public override void PhysicsUpdate() { base.PhysicsUpdate(); } } ``` ### Update entity and other state<a id="US"/> **Entity** ```csharp= public virtual bool CheckPlayerInMinAgroRange() { return Physics2D.Raycast(playerCheck.position, aliveGameObject.transform.right, entityData.minAgroDistance, entityData.whatIsPlayer); } public virtual bool CheckPlayerInMaxAgroRange() { return Physics2D.Raycast(playerCheck.position, aliveGameObject.transform.right, entityData.maxAgroDistance, entityData.whatIsPlayer); } ``` **MoveState(other state)** ```csharp= //MoveState public class MoveState : State { ~~~ protected bool isPlayerMinAgroRange; public override void Enter() { ~~~ isPlayerMinAgroRange = entity.CheckPlayerInMinAgroRange(); } public override void PhysicsUpdate() { ~~~ isPlayerMinAgroRange = entity.CheckPlayerInMinAgroRange(); } } //E1_MoveState public class E1_MoveState : MoveState { ~~~ public override void LogicUpdate() { ~~~ if (isPlayerMinAgroRange) { stateMachine.ChangeState(enermy.playerDetectedState); } } } ```