---
# System prepended metadata

title: Unity Platform 14 Enermy with StateMachine
tags: [platform]

---

Unity Platform 14 Enermy with StateMachine
===
2020.08.25

###### tags: `platform`

## List
1. [Enermy Script](#ES)
2. [Each Staet](#ES2)

## Content
### Enermy Script<a id="ES"/>
Main Script is `Enemry(E1)` inheritatied Entity.<br>Each states made by.
```csharp=
public class E1 : Entity
{
    public E1_IdleState idleState { get; private set; }
    public E1_MoveState moveState { get; private set; }

    [SerializeField]
    private D_IdleState idleStateData;
    [SerializeField]
    private D_MoveState moveStateData;

    public override void Start()
    {
        base.Start();

        moveState = new E1_MoveState(this, stateMachine, "move", moveStateData, this);
        idleState = new E1_IdleState(this, stateMachine, "idle", idleStateData, this);

        stateMachine.initialize(moveState);
    }
}
```

### Each State<a id="ES2"/>
**Move State**
```csharp=
public class E1_MoveState : MoveState
{
	private E1 enermy;

	public E1_MoveState(Entity entity, FiniteStateMachine stateMachine, string animBoolName, D_MoveState 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 (isDetectingWall || !isDetectingLedge)
		{
			enermy.idleState.SetFlipAfterIdle(true);
			stateMachine.ChangeState(enermy.idleState);
		}
	}

	public override void PhysicsUpdate()
	{
		base.PhysicsUpdate();
	}
}
```

**Idle State**
```csharp=
public class E1_IdleState : IdleState
{
    private E1 enermy;

	public E1_IdleState(Entity entity, FiniteStateMachine stateMachine, string animBoolName, D_IdleState 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 (isIdleTimeOver)
        {
            stateMachine.ChangeState(enermy.moveState);
        }
	}

	public override void PhysicsUpdate()
	{
		base.PhysicsUpdate();
	}	
}
```