Book Image

Building a Game with Unity and Blender

Book Image

Building a Game with Unity and Blender

Overview of this book

In the wake of the indie game development scene, game development tools are no longer luxury items costing up to millions of dollars but are now affordable by smaller teams or even individual developers. Among these cutting-edge applications, Blender and Unity stand out from the crowd as a powerful combination that allows small-to-no budget indie developers or hobbyists alike to develop games that they have always dreamt of creating. Starting from the beginning, this book will cover designing the game concept, constructing the gameplay, creating the characters and environment, implementing game logic and basic artificial intelligence, and finally deploying the game for others to play. By sequentially working through the steps in each chapter, you will quickly master the skills required to develop your dream game from scratch.
Table of Contents (16 chapters)
Building a Game with Unity and Blender
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Improving enemy AI


In the previous chapter, we learned how to create enemies that will chase the player if too close with it. However, the enemies look very stiff because they don't move at all when the player is not within its sight. Therefore, we will adjust the AI script a little bit to make the enemies able to patrol around the level randomly if the player is not detected.

First of all, open up the EnemyAI script and add these variables on top:

Vector3 randomPos;
float pauseInterval = 0;
float pauseTime = 0;

The randomPos variable is where the enemy will be moving to during patrolling. The randomPos variable will be recalculated if the enemy has reached that position. The enemy will stand still for a while before moving to the next random position.

Next, add CalculateRandomPosition() to the Start() function:

void Start ()
{
  // Find player
  player = GameObject.FindWithTag ("Player");
  CalculateRandomPosition ();
}

This is how the CalculateRandomPosition() function looks:

void CalculateRandomPosition...