Book Image

Unity Game Development Scripting

By : Kyle D'Aoust
Book Image

Unity Game Development Scripting

By: Kyle D'Aoust

Overview of this book

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

Putting it all together


To wrap up our AI package, we will now finish up the script and add it to the skeleton.

Final coding touches

At the beginning of our AI script, we created some variables that we have yet to properly assign. We will do that in the Start function. We will also add the Update function to run our AI code. Add these functions to the bottom of the class:

void Start()
{
  navAgent = GetComponent<NavMeshAgent>();

  Stats.Add(new KeyValuePair<string, int>("Health", 100));
  Stats.Add(new KeyValuePair<string, int>("Speed", 10));
  Stats.Add(new KeyValuePair<string, int>("Damage", 25));
  Stats.Add(new KeyValuePair<string, int>("Agility", 25));
  Stats.Add(new KeyValuePair<string, int>("Accuracy", 60));
}

void Update ()
{
  RunBehaviors();
}

In the Start function, we first assign the navAgent variable by getting the NavMeshAgent component from the GameObject. Next, we add new KeyValuePair variables to the Stats array. The Stats array is now...