Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Building a Game with Unity and Blender

You're reading from   Building a Game with Unity and Blender Learn how to build a complete 3D game using the industry-leading Unity game development engine and Blender, the graphics software that gives life to your ideas

Arrow left icon
Product type Paperback
Published in Nov 2015
Publisher
ISBN-13 9781785282140
Length 250 pages
Edition 1st Edition
Tools
Arrow right icon
Toc

Table of Contents (11) Chapters Close

Preface 1. Creating Your Game Concept 2. Creating Characters FREE CHAPTER 3. Animating Your Characters 4. Creating the Environment 5. Integrating Your Assets into the Game 6. Developing the Game Structure 7. Creating Levels and Game Progression 8. Post-Production and Visual FX 9. Deploying the Game 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...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime