CategoryUnity

Unity: Using an FSM to control your GameObjects

Thanks to my good friend Maurizio, who’s been helping me removing the dust from my memory, today I’ll show you how to use a simple FSM to control your GameObjects šŸ™‚

First, if you don’t know what an FSM is, please read this, and maybe this too.

Done? Good. Now take a look at thisĀ (use arrows and spacebar for jumping).

See? It’s just Mario walking on a small platform. He can jump (obviously, he’s Mario) and can fall from the borders.

There are tons of ways to implement this and one of them is using an FSM.

The Player Ā and the bricks have only a BoxCollider2D, no Physics components. On the Player I have added a Script component that acts as a controller (code is on GitHub).

The idea is to let the controller initialize the FSM adding the needed States and define for each state the TransitionsĀ (basically destination State and condition ).

For this demo I have implemented three States: Default, Jumping and Falling. The transitions are simple:

FSM Transitions

  • Default -> Jump: on “spacebar” pressed
  • Default -> Falling: if no collision occurs on -Y
  • Jump -> Falling: after 0.5 seconds
  • Falling -> Default: if a collision occurs on -Y

The states themselves are pretty simple:

  • Default:Ā checks Ā for input onĀ the horizontal axis and moves the player
  • Jump: adds an impulse to the Y of the player
  • Falling: applies gravity to the player transform

How to reference an array of scripts from another script in Unity

Just a quick and dirty tip (actually, more of a reminder for myself), probably there’s a better way but for now it worked for me… Basically in Unity I needed a quick way to reference a list of scripts from another script. The idea is to have a “main” script holdingĀ a list of sub-scripts used to createĀ instances of different game objects (some kind of dynamic factory). The first stepĀ is to createĀ a base class for the sub scripts:

[csharp]

using UnityEngine;

public abstract class HazardCreator : MonoBehaviour {
public abstract GameObject CreateHazard ();
}

[/csharp]

then some subclasses:

[csharp]

public class EnemyCreator : HazardCreator {
public override GameObject CreateHazard(){ ….. }
}

public class AsteroidCreator: HazardCreator {
public override GameObject CreateHazard(){ ….. }
}

[/csharp]

then in the game hierarchy I created a GameObject used as “controller” and a child GameObject that contains all the factory scripts. As last step I assigned a script to the “controller” GameObject:

[csharp]

public class GameController : MonoBehaviour {
void Start ()
{
var factories = this.GetComponentsInChildren<HazardCreator>();
if(null != factories && 0 != factories.Length)
Debug.Log("Found " + factories.Length + " hazard creators");
}
}

[/csharp]

As you may easily notice, the call to GetComponentsInChildren is helping us getting all the scripts inheriting from the HazardCreator base class šŸ™‚

 

 

© 2017 Davide Guida

Theme by Anders NorenUp ↑