-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathStateMachine.cs
More file actions
34 lines (29 loc) · 742 Bytes
/
StateMachine.cs
File metadata and controls
34 lines (29 loc) · 742 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using UnityEngine;
namespace UnityCheatSheet.Patterns.StatePattern
{
public class StateMachine : MonoBehaviour
{
private IState currentState;
private void Start()
{
// Start with the first state in your sequence
ChangeState(new States.MovementTutorialState(this));
}
private void Update()
{
if (currentState != null)
{
currentState.Update();
}
}
public void ChangeState(IState newState)
{
if (currentState != null)
{
currentState.Exit();
}
currentState = newState;
currentState.Enter();
}
}
}