To quickly test our implementation of the State pattern in your own instance of Unity, you need to follow these steps:
- Copy all the scripts we just reviewed inside your Unity project.
- Create a new empty scene.
- Add a 3D GameObject to the scene, such as a cube, ensuring that it's visible to the main camera.
- Attach the BikeController script to the GameObject.
- Also attach the following client script to the GameObject:
using UnityEngine;
namespace Chapter.State
{
public class ClientState : MonoBehaviour
{
private BikeController _bikeController;
void Start()
{
_bikeController =
(BikeController)
FindObjectOfType(typeof(BikeController));
}
void OnGUI()
{
if (GUILayout.Button("Start Bike"))
_bikeController.StartBike();
if (GUILayout.Button("Turn Left"))
_bikeController.Turn(Direction...