Skip to Main Content
Unity 2018 By Example - Second Edition
book

Unity 2018 By Example - Second Edition

by Alan Thorn
July 2018
Beginner content levelBeginner
484 pages
9h 36m
English
Packt Publishing
Content preview from Unity 2018 By Example - Second Edition

Creating Intelligent Enemies – FSMs

In this section, we'll define the code to work with the enemy prefab; specifically, the FSM defining its core behavior. The enemy, once spawned in the level, will enter chase mode, causing it to follow the player, wherever they may be. On reaching the player, the enemy will attack and cause damage.

The enemy AI is encoded in the BotAI.cs script file. See the following code sample:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class BotAI : MonoBehaviour { public enum AISTATE { CHASE = 0, ATTACK = 1 }; public AISTATE CurrentState { get { return _CurrentState; } set { StopAllCoroutines(); _CurrentState = value; switch(_CurrentState) { case AISTATE.CHASE: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Unity 2020 By Example - Third Edition

Unity 2020 By Example - Third Edition

Robert Wells
Unity 2018 Cookbook - Third Edition

Unity 2018 Cookbook - Third Edition

Matt Smith, Chico Queiroz, Jate Wittayabundit

Publisher Resources

ISBN: 9781788398701Supplemental Content