Game Development Reference
In-Depth Information
Unity Hands On
Fuzzy Logic
This hands-on session uses the DotFuzzy engine to create behavior rules
for an NPC. The NPC's health and shield strength will be used to calculate
its mood. Depending on its mood, it will decide to chase, attack, or ignore
the player who is able to shoot at it.
Step 1. Download Chapter Five/ from the Web site
and open the fuzzyAI scene. Play. You can walk around the scene and
shoot spheres from the large gun being held by the player. In the
middle of the environment you will find a robot. This robot has similar
code to that used in the Line of Sight workshop; however, it will not
attack you. The robot's health and shield strengths are displayed on
the screen. If you shoot at the robot with the left mouse button, its
health and shield strength will go down. If you leave the robot alone,
it will recover after some time.
The DotFuzzy engine has been loaded and is in the Project Plugins folder.
Step 2. Open the fuzzyBrain JavaScript file with the script editor. The
linguistic variables for the engine are defined at the top. Add the code
as shown in Listing 5.21 .
Listing 5.21 Implementing a Fuzzy Logic Engine in Unity
import DotFuzzy;
var moodValue: float = 100;
var explode: GameObject;
var health: LinguisticVariable;
var shield: LinguisticVariable;
var mood: LinguisticVariable;
var fuzzyEngine: FuzzyEngine;
var botHealth: int = 100;
var botShield: int = 100;
private var updateTime = 2; //seconds
private var lastUpdate = 0;
function OnCollisionEnter(obj: Collision)
var e = Instantiate(explode,
this.transform.position, Quaternion.identity);
botShield -= 10;
Search Nedrilad ::

Custom Search