---
# System prepended metadata

title: Unity Platform 13 State Machine Update
tags: [platform]

---

Unity Platform 13 State Machine Update
===
2020.08.25

###### tags: `platform`

## List
1. [Data](#D)
2. [Check Surroundings](#CS)
3. [State Inheritance](#SI)

## Content
### Data<a id="D"/>
Wall and ledge check are also needed in any objs.<br>It use some attributes.<br>They are saved in Data Struture.<br>`CreateAssetMenu` makes asset in asset menu.
```csharp=
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "newEntityData", menuName = "Data/Entity Data/Base Data")]

public class D_Entity : ScriptableObject
{
    public float wallCheckDistance = 0.2f;
    public float ledgeCheckDistance = 0.4f;

    public LayerMask whatIsGround;
}

```

### Check Surroundings<a id="CS"/>
Wall Check & Ledge Check are same in player.<br>These functions are used in State.
```csharp=
public class Entity : MonoBehaviour
{
    ~~~
    public int facingDirection { get; private set; }
    public D_Entity entityData;

    // For Checking
    [SerializeField]
    private Transform wallCheck;
    [SerializeField]
    private Transform ledgeCheck;

    //Check Wall and Ledge function
    public virtual bool CheckWall()
    {
        return Physics2D.Raycast(wallCheck.position, aliveGameObject.transform.right, entityData.wallCheckDistance, entityData.whatIsGround);
    }
    public virtual bool CheckLedge()
    {
        return Physics2D.Raycast(ledgeCheck.position, Vector2.down, entityData.ledgeCheckDistance, entityData.whatIsGround);
    }
    
    public virtual void Flip()
    {
        facingDirection *= -1;
        aliveGameObject.transform.Rotate(0f, 180f, 0f);
    }
}
```

### State Inheritace<a id="SI"/>
State is various, moveState, IdleState, etc...<br>These are inheritatied of State class.<br>Look below, any logic during moving<br>
**MoveState**
```csharp=
public class MoveState : State
{
    protected D_MoveState stateData;

	protected bool isDetectingWall;
	protected bool isDetectingLedge;

	public MoveState(Entity entity, FiniteStateMachine stateMachine, string animBoolName, D_MoveState stateData) : base(entity, stateMachine, animBoolName)
	{
        this.stateData = stateData;
	}

    public override void Enter()
    {
        base.Enter();
        entity.SetVelocity(stateData.movementSpeed);

		isDetectingWall = entity.CheckWall();
		isDetectingLedge = entity.CheckLedge();
    }


	public override void Exit()
	{
		base.Exit();
	}


	public override void LogicUpdate()
	{
		base.LogicUpdate();
	}

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

		isDetectingWall = entity.CheckWall();
		isDetectingLedge = entity.CheckLedge();
	}
}
```

**Idle State**
```csharp=
public class IdleState : State
{
    protected D_IdleState stateData;

    protected bool flipAfterIdle;
	protected bool isIdleTimeOver;

	protected float idleTime;

	public IdleState(Entity entity, FiniteStateMachine stateMachine, string animBoolName, D_IdleState stateData) : base(entity, stateMachine, animBoolName)
	{
        this.stateData = stateData;
	}

	public override void Enter()
	{
		base.Enter();
        entity.SetVelocity(0f);
		isIdleTimeOver = false;
		SetRandomIdleTime();
	}

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

		if (flipAfterIdle)
		{
			entity.Flip();
		}
	}	

	public override void LogicUpdate()
	{
		base.LogicUpdate();
		if (Time.time >= startTime + idleTime)
		{
			isIdleTimeOver = true;
		}
	}

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

    public void SetFlipAfterIdle(bool flip)
    {
        flipAfterIdle = flip;
    }

	private void SetRandomIdleTime()
	{
		idleTime = Random.Range(stateData.minIdleTime, stateData.maxIdleTime);
	}
}

```