Book Image

Mastering Unity Scripting

By : Alan Thorn
Book Image

Mastering Unity Scripting

By: Alan Thorn

Overview of this book

Table of Contents (17 chapters)
Mastering Unity Scripting
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating the Seek-Health (or flee) state


The Seek-Health state occurs when the enemy runs low on health and can restore it by collecting a medikit. This state is unlike most others, in that, it can be reached or entered from any state. The entering of this state doesn't depend on its relationship to others, but only on the player's health. Specifically, this state should be entered when the enemy's health is reduced beyond a minimum floor. As a result of this configuration, be sure to hook up the Seek-Health animation state in the Mecanim graph to the Any State node that allows the run animation to be triggered in any state, as shown here:

The Seek-Health state can be accessed from Any State

Each enemy maintains a Health variable; this variable is adjusted either up or down, depending on whether the enemy finds a medikit or is attacked. The change occurs inside the method ChangeHealth, and this is where we can determine whether a SeekHealth state must be initiated. The ChangeHealth function...