Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Android Game Development with Unity

You're reading from   Mastering Android Game Development with Unity Build high-end Android games with Unity's advanced features

Arrow left icon
Product type Paperback
Published in May 2017
Publisher Packt
ISBN-13 9781783550777
Length 352 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Siddharth Shekar Siddharth Shekar
Author Profile Icon Siddharth Shekar
Siddharth Shekar
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Introduction to Android Game Development with Unity3D FREE CHAPTER 2. Finishing the Perky Penguin 2D Game 3. Adding Player Character for an Action Fighting Game 4. Enemy Characters with AI 5. Gameplay, UI, and Effects 6. GameScene and SceneFlow 7. Gamestats, Social, IAP, and Ad Integration 8. Sound, Finishing Touches, and Publishing

Enemy behavior and AI


The enemy will start with the idle animation as you play the game. Let's add some behavior to the enemy character.

The AI is implemented by creating a pattern, which will determine when the next state change for the enemy will take place, and the period of time that the enemy will either be idle, defend, or attack.

The pattern is an array and has 20 elements in it. After all the elements are exhausted the patterns is randomized and the counter is set to 0 again.

Some intervals last for 10 frames, while others last for up to 120 frames or two seconds. The player has to judge and make sure that he doesn't get hit, and he has to defeat the enemy before they kill him. This will make more sense once we go through the code.

Create a new C# script and name it enemy. Add the following script to the code.

First, we initialize our variables:

    using UnityEngine; 
    using System.Collections; 

    public class enemy : MonoBehaviour { 

    private Animator anim; 

    int myTick...
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
Banner background image