Game Development Reference
In-Depth Information
The FSM works by entering a state and then transitioning from enter, through
update to exit. At the end of each event, the event is updated such that the
next loop the FSM runs the code goes into the next event. On exiting a state,
the state value is changed to something else and the event is set to enter.
Unity Hands On
Implementing an FSM
In this workshop you will learn how to implement an FSM in conjunction
with a waypoint system and the A* algorithm to program a guard
character that patrols a warehouse and pursues intruders.
Step 1. Download Chapter Five/FSM.zip from the Web site and open
the scene warehousePatrol. In the Scene you will see the warehouse
model, the robot from the last workshop, and a waypoint system
of spheres.
Step 2. Create a new tag named waypoint and set it for the tag
of each sphere.
Step 3. Create a new JavaScript file called patrol and open with the
script editor. Enter the code in Listing 5.9 and attach it to the robot.
Listing 5.9 Setting up a Waypoint System with Automatic
Waypoint Collection
private var graph: Graph = new Graph();
private var currentWP: int = 0;
private var currentNode: GameObject;
private var speed: int = 8;
private var rotationSpeed: int = 5;
private var accuracy: Number = 1;
function CreateBiPath(a: String, b: String)
{
var w1 = GameObject.Find(a);
var w2 = GameObject.Find(b);
if(w1 && w2) //if both objects exist
{
//create edges between the waypoints
//in both directions
graph.AddEdge(w1, w2);
graph.AddEdge(w2, w1);
}
}
function Start ()
{
//automatically find all game objects with tag waypoint
//and add to the graph
var gos : GameObject[];
Search Nedrilad ::




Custom Search