100% found this document useful (1 vote)
25 views

Unity AI Game Programming Leverage the power of Unity 5 to create stunningly life like AI entities in your games 2nd Edition Ray Barreradownload

The document provides links to download various ebooks and textbooks related to game programming and development, particularly focusing on Unity and AI. It highlights titles such as 'Unity AI Game Programming' and 'Mastering Unity Shaders and Effects,' along with author information and publication details. Additionally, it includes a brief overview of the authors and reviewers involved in the creation of the books.

Uploaded by

iudawiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
25 views

Unity AI Game Programming Leverage the power of Unity 5 to create stunningly life like AI entities in your games 2nd Edition Ray Barreradownload

The document provides links to download various ebooks and textbooks related to game programming and development, particularly focusing on Unity and AI. It highlights titles such as 'Unity AI Game Programming' and 'Mastering Unity Shaders and Effects,' along with author information and publication details. Additionally, it includes a brief overview of the authors and reviewers involved in the creation of the books.

Uploaded by

iudawiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Download the full version and explore a variety of ebooks

or textbooks at https://ebookultra.com

Unity AI Game Programming Leverage the power of


Unity 5 to create stunningly life like AI entities
in your games 2nd Edition Ray Barrera

_____ Tap the link below to start your download _____

https://ebookultra.com/download/unity-ai-game-programming-
leverage-the-power-of-unity-5-to-create-stunningly-life-
like-ai-entities-in-your-games-2nd-edition-ray-barrera/

Find ebooks or textbooks at ebookultra.com today!


We have selected some products that you may be interested in
Click the link to download now or visit ebookultra.com
for more options!.

Mastering Unity Shaders and Effects Harness the power of


Unity 5 tools to write shaders and create stunning effects
for next generation games 1st Edition Jamie Dean
https://ebookultra.com/download/mastering-unity-shaders-and-effects-
harness-the-power-of-unity-5-tools-to-write-shaders-and-create-
stunning-effects-for-next-generation-games-1st-edition-jamie-dean/

AI Game Programming Wisdom 4 1st Edition Steve Rabin (Ed.)

https://ebookultra.com/download/ai-game-programming-wisdom-4-1st-
edition-steve-rabin-ed/

AI for Game Developers 1st ed Edition David M. Bourg

https://ebookultra.com/download/ai-for-game-developers-1st-ed-edition-
david-m-bourg/

Unity in the Book of Isaiah 1st Edition Benedetta Rossi

https://ebookultra.com/download/unity-in-the-book-of-isaiah-1st-
edition-benedetta-rossi/
A Sacred Unity Further Steps to an Ecology of Mind 2nd
Edition Gregory Bateson

https://ebookultra.com/download/a-sacred-unity-further-steps-to-an-
ecology-of-mind-2nd-edition-gregory-bateson/

HTML5 Game Development by Example 2nd Edition Make the


most of HTML5 techniques to create exciting games from
scratch Makzan
https://ebookultra.com/download/html5-game-development-by-example-2nd-
edition-make-the-most-of-html5-techniques-to-create-exciting-games-
from-scratch-makzan/

AI Driven Project Management Harnessing the Power of


Artificial Intelligence and ChatGPT 1st Edition Bainey

https://ebookultra.com/download/ai-driven-project-management-
harnessing-the-power-of-artificial-intelligence-and-chatgpt-1st-
edition-bainey/

Nana Vol 15 Ai Yazawa

https://ebookultra.com/download/nana-vol-15-ai-yazawa/

Nana Vol 4 Ai Yazawa

https://ebookultra.com/download/nana-vol-4-ai-yazawa/
Unity AI Game Programming Leverage the power of
Unity 5 to create stunningly life like AI entities in your
games 2nd Edition Ray Barrera Digital Instant Download
Author(s): Ray Barrera, Aung Sithu Kyaw, Clifford Peters, Thet Naing Swe
ISBN(s): 9781849695305, 1849693404
Edition: 2
File Details: PDF, 3.01 MB
Year: 2015
Language: english
[1]
Unity AI Game Programming
Second Edition

Leverage the power of Unity 5 to create stunningly


life-like AI entities in your games!

Ray Barrera
Aung Sithu Kyaw
Clifford Peters
Thet Naing Swe

BIRMINGHAM - MUMBAI
Unity AI Game Programming
Second Edition

Copyright © 2015 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy
of the information presented. However, the information contained in this book is
sold without warranty, either express or implied. Neither the authors, nor Packt
Publishing, and its dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.

First published: July 2013

Second edition: September 2015

Production reference: 1180915

Published by Packt Publishing Ltd.


Livery Place
35 Livery Street
Birmingham B3 2PB, UK.

ISBN 978-1-78528-827-2

www.packtpub.com
Credits

Authors Copy Editor


Ray Barrera Swati Priya
Aung Sithu Kyaw
Clifford Peters Project Coordinator
Milton Dsouza
Thet Naing Swe

Proofreader
Reviewers
Safis Editing
Mohammedun Bakir Bagasrawala
Adam Boyce
Indexer
Jack Donovan
Monica Ajmera Mehta
Chaima Jemmali
Akshay Sunil Masare Production Coordinator
Arvindkumar Gupta
Commissioning Editor
Kartikey Pandey Cover Work
Arvindkumar Gupta
Acquisition Editors
Manish Nainani
Llewellyn Rozario

Content Development Editor


Rashmi Suvarna

Technical Editors
Manal Pednekar
Ankita Thakur
About the Authors

Ray Barrera was a tinker in his childhood. From making mods and custom
maps in games such as StarCraft and Unreal Tournament to developing open
source role-playing games using RPG Maker, he always had a passion for game
development. The passion stayed with him, and after many years as a hobbyist,
he decided to take the plunge into professional development.

In the initial stages of his career, he was fortunate enough to work on educational and
research projects for major contractors in the defense industry, allowing him to blend
his love for games with his innate desire to teach and create interactive experiences.
Since then, he has straddled the line between entertainment and education. Unity
was the logical weapon of choice for him as it gave him the flexibility to create games
and applications and iterate quickly. From being an original member of the Los
Angeles Unity meetup to helping coordinate Unity workshops at local colleges and
high schools, he has been very active in the Unity community. You can follow him on
Twitter at @ray_barrera.

There are too many people to name, but I'd like to thank the team
at Packt Publishing for this exciting opportunity, and of course, my
wonderful friends and family, especially my parents, who always
encouraged me to follow my passion and supported me along
every step of the way. I'd also like to thank the Twistory team for
being such an amazing group of people—Danny, JP, DW, Richard,
the lovely "Purple", and everyone else—whom I was so fortunate
to work with. Thanks to Peter Trennum for the mentorship and
leadership he has provided at this stage in my career. Lastly,
I'd like to thank Gianni, my brother, for all the love and
support over the years.
Aung Sithu Kyaw has been in the technical industry for over a decade. He is
passionate about graphics programming, creating video games, writing, and sharing
knowledge with others. He holds an MSc in digital media technology from the
Nanyang Technological University (NTU), Singapore. Over the last few years, he
has worked in various positions, including research programmer and senior game
programmer. Lastly, he worked as a research associate, which involved implementing
a sensor-based real-time movie system using Unreal Development Kit. In 2011, he
founded a tech start-up, which focuses on interactive media productions and backend
server-side technologies. He is currently based in Myanmar and working on his latest
company's product, a gamified social opinion network for Myanmar.

He can be followed on Twitter at @aungsithu and LinkedIn at http://linkedin.


com/in/aungsithu.

Thanks to my coauthors who worked really hard with me on


this book despite their busy schedules and helped get this book
published. Thanks also goes to the team at Packt Publishing for
having us produce this book. And finally, thanks to the awesome
guys at Unity3D for building this amazing toolset and making it
affordable to indie game developers. Dedicated to L!

Clifford Peters is a programmer and a computer scientist. He was the technical


reviewer for Unity Game Development Essentials, Unity 3D Game Development by
Example Beginner's Guide, Unity 3 Game Development HOTSHOT, Unity 3.x Game
Development by Example Beginner's Guide, Unity iOS Game Development Beginner's
Guide, and Unity iOS Essentials, all by Packt Publishing.
Thet Naing Swe is the founder and CTO of Joy Dash Pte Ltd, based in Singapore.
He graduated from the University of Central Lancashire with a major in game
design and development and started his career as a game programmer at one of
the UK-based Nintendo DS game development studios. In 2010, he relocated to
Singapore and worked as a graphics programmer at the Nanyang Technological
University (NTU) on a cinematic research project.

At Joy Dash, he's responsible for interactive digital media consulting projects,
especially in education, casual games, and augmented reality projects using
Unity 3D as the main development tool. He can be reached via thetnswe@gmail.com.

I would like to thank the whole team at Packt Publishing for


keeping track of all the logistics and making sure the book was
published no matter what; I really appreciate this. I'd also like to
thank my parents for supporting me all these years and letting me
pursue my dream of becoming a game developer. Without all your
support, I wouldn't be here today.

And finally, a huge thanks to my wife, May Thandar Aung, for


allowing me to work on this book after office hours, late at night, and
even on weekends. Without your understanding and support, this
book would have been delayed for another year. I'm grateful to have
your support in whatever I do. I love you.
About the Reviewers

Mohammedun Bakir Bagasrawala is a Unity AI engineer at Beachhead Studio,


an Activision Blizzard studio. He holds a master's degree in computer science with
a specialization in game development from the University of Southern California.
He worked at DreamWorks Animation, where he was part of the team that built
innovative AI technologies. He then moved to Treyarch and had the utmost pleasure
of working on Call of Duty: Black Ops 3, implementing several features of this game.
Apart from his professional experience, he has also been an AI lead across a gamut of
mobile, console, and board games at the USC GamePipe Laboratory.

I would like to thank my parents, Shabbir and Rita; my siblings,


Esmail and Jacklyn; and my best friend, Afreen, for helping me
become who I am today. I would also like to thank Giselle, Pratik,
Rushabh, Neel, Soham, Kashyap, Sabarish, and Alberto as they have
stood by me throughout. Lastly, I would like to thank my former
managers, Mark, Vishwa, Ryan, and Trevor and my professors,
Artem and Michael Zyda.

Adam Boyce is a software developer and an independent game


developer who specializes in C# scripting, game design, and AI development.
His experience includes application support, software development, and data
architecture with various Canadian corporations. He was also the technical
reviewer for Unity AI Programming Essentials, Packt Publishing. You can read
his development blog at www.gameovertures.ca and follow him on Twitter
at https://twitter.com/AdamBoyce4.

I'd like to thank my wife, Gail, for supporting me throughout the


review process and also in my life and career.
Jack Donovan is a game developer and software engineer who has been working
with the Unity3D engine since its third major release. He studied at Champlain
College in Burlington, Vermont, where he received a BS in game programming.

He currently works at IrisVR, a virtual reality start-up in New York City, and
develops software that allows architects to generate virtual reality experiences from
their CAD models or blueprints. Prior to this company, he worked as part of a small
independent game team with fellow students, and that was when he wrote OUYA
Game Development by Example Beginner's Guide, Packt Publishing.

Chaima Jemmali holds an engineering degree in networks and telecommunication.


Currently, she is a Fulbright scholar, pursuing a master's degree in interactive
media and game development at the Worcester Polytechnic Institute, Worcester,
Massachusetts.

She has always wanted to share her love for programming through her master's
project, which is a serious game that teaches coding, her internship as an instructor
with iD Tech Camps, and by contributing to the success of this book.

I would like to thank the writers and everyone who worked hard to
help produce this book.

Akshay Sunil Masare is currently a student at the Indian Institute of Technology,


Kanpur, working toward his BTech in computer science and engineering. He has
developed various games on Android and also on the Web. He has also worked on
an AI agent that uses deep learning and convolutional neural networks to learn and
train itself to play any game on the Atari 2600 platform.
www.PacktPub.com

Support files, eBooks, discount offers, and more


For support files and downloads related to your book, please visit www.PacktPub.com.

Did you know that Packt offers eBook versions of every book published, with PDF
and ePub files available? You can upgrade to the eBook version at www.PacktPub.com
and as a print book customer, you are entitled to a discount on the eBook copy. Get in
touch with us at service@packtpub.com for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign
up for a range of free newsletters and receive exclusive discounts and offers on Packt
books and eBooks.
TM

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital
book library. Here, you can search, access, and read Packt's entire library of books.

Why subscribe?
• Fully searchable across every book published by Packt
• Copy and paste, print, and bookmark content
• On demand and accessible via a web browser

Free access for Packt account holders


If you have an account with Packt at www.PacktPub.com, you can use this to access
PacktLib today and view 9 entirely free books. Simply use your login credentials for
immediate access.
Table of Contents
Preface v
Chapter 1: The Basics of AI in Games 1
Creating the illusion of life 1
Leveling up your game with AI 3
Using AI in Unity 4
Defining the agent 4
Finite State Machines 4
Seeing the world through our agent's eyes 6
Path following and steering 7
Using A* Pathfinding 8
Using navigation mesh 10
Flocking and crowd dynamics 13
Behavior trees 13
Thinking with fuzzy logic 16
Summary 16
Chapter 2: Finite State Machines and You 17
Finding uses for FSMs 17
Creating state machine behaviors 19
Creating the AnimationController asset 19
Layers and Parameters 21
The animation controller inspector 23
Bringing behaviors into the picture 23
Creating our very first state 23
Transitioning between states 25
Setting up our player tank 25
Creating the enemy tank 25
Choosing transitions 26

[i]
Table of Contents

Making the cogs turn 28


Setting conditions 30
Driving parameters via code 33
Making our enemy tank move 36
Testing 39
Summary 39
Chapter 3: Implementing Sensors 41
Basic sensory systems 42
Cone of sight 42
Hearing, feeling, and smelling using spheres 43
Expanding AI through omniscience 44
Getting creative with sensing 45
Setting up the scene 45
Setting up the player tank and aspect 47
Implementing the player tank 48
Implementing the Aspect class 50
Creating an AI character 50
Using the Sense class 52
Giving a little perspective 53
Touching is believing 55
Testing the results 58
Summary 59
Chapter 4: Finding Your Way 61
Following a path 62
The path script 64
Using the path follower 65
Avoiding obstacles 68
Adding a custom layer 70
Implementing the avoidance logic 71
A* Pathfinding 75
Revisiting the A* algorithm 75
Implementation 77
Implementing the Node class 77
Establishing the priority queue 78
Setting up our grid manager 79
Diving into our A* implementation 85
Implementing a Test Code class 88
Setting up our sample scene 90
Testing all the components 94

[ ii ]
Table of Contents

Navigation mesh 96
Setting up the map 96
Navigation Static 97
Baking the navigation mesh 98
Using the NavMesh agent 102
Setting a destination 103
The Target class 104
Testing slopes 105
Exploring areas 107
Making sense of Off Mesh Links 109
Using the generated Off Mesh Links 110
Setting the manual Off Mesh Links 111
Summary 113
Chapter 5: Flocks and Crowds 115
Learning the origins of flocks 115
Understanding the concepts behind flocks and crowds 116
Flocking using Unity's samples 118
Mimicking individual behavior 119
Creating the controller 126
Using an alternative implementation 128
Implementing the FlockController 130
Using crowds 135
Implementing a simple crowd simulation 136
Using the CrowdAgent component 138
Adding some fun obstacles 140
Summary 143
Chapter 6: Behavior Trees 145
Learning the basics of behavior trees 145
Understanding different node types 146
Defining composite nodes 147
Understanding decorator nodes 148
Describing the leaf node 149
Evaluating the existing solutions 149
Implementing a basic behavior tree framework 150
Implementing a base Node class 150
Extending nodes to selectors 151
Moving on to sequences 152
Implementing a decorator as an inverter 154
Creating a generic action node 155

[ iii ]
Table of Contents

Testing our framework 156


Planning ahead 157
Examining our scene setup 158
Exploring the MathTree code 159
Executing the test 163
Summary 166
Chapter 7: Using Fuzzy Logic to Make Your AI Seem Alive 167
Defining fuzzy logic 167
Picking fuzzy systems over binary systems 169
Using fuzzy logic 169
Implementing a simple fuzzy logic system 170
Expanding the sets 179
Defuzzifying the data 179
Using the resulting crisp data 181
Using a simpler approach 182
Finding other uses for fuzzy logic 183
Merging with other concepts 183
Creating a truly unique experience 184
Summary 184
Chapter 8: How It All Comes Together 185
Setting up the rules 185
Creating the towers 186
Making the towers shoot 194
Setting up the tank 196
Setting up the environment 200
Testing the example 202
Summary 203
Index 205

[ iv ]
Exploring the Variety of Random
Documents with Different Content
If the second copy is also defective, you may demand a refund
in writing without further opportunities to fix the problem.

1.F.4. Except for the limited right of replacement or refund set


forth in paragraph 1.F.3, this work is provided to you ‘AS-IS’,
WITH NO OTHER WARRANTIES OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of
damages. If any disclaimer or limitation set forth in this
agreement violates the law of the state applicable to this
agreement, the agreement shall be interpreted to make the
maximum disclaimer or limitation permitted by the applicable
state law. The invalidity or unenforceability of any provision of
this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the


Foundation, the trademark owner, any agent or employee of the
Foundation, anyone providing copies of Project Gutenberg™
electronic works in accordance with this agreement, and any
volunteers associated with the production, promotion and
distribution of Project Gutenberg™ electronic works, harmless
from all liability, costs and expenses, including legal fees, that
arise directly or indirectly from any of the following which you
do or cause to occur: (a) distribution of this or any Project
Gutenberg™ work, (b) alteration, modification, or additions or
deletions to any Project Gutenberg™ work, and (c) any Defect
you cause.

Section 2. Information about the Mission


of Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new
computers. It exists because of the efforts of hundreds of
volunteers and donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project
Gutenberg™’s goals and ensuring that the Project Gutenberg™
collection will remain freely available for generations to come. In
2001, the Project Gutenberg Literary Archive Foundation was
created to provide a secure and permanent future for Project
Gutenberg™ and future generations. To learn more about the
Project Gutenberg Literary Archive Foundation and how your
efforts and donations can help, see Sections 3 and 4 and the
Foundation information page at www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-
profit 501(c)(3) educational corporation organized under the
laws of the state of Mississippi and granted tax exempt status
by the Internal Revenue Service. The Foundation’s EIN or
federal tax identification number is 64-6221541. Contributions
to the Project Gutenberg Literary Archive Foundation are tax
deductible to the full extent permitted by U.S. federal laws and
your state’s laws.

The Foundation’s business office is located at 809 North 1500


West, Salt Lake City, UT 84116, (801) 596-1887. Email contact
links and up to date contact information can be found at the
Foundation’s website and official page at
www.gutenberg.org/contact
Section 4. Information about Donations to
the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission
of increasing the number of public domain and licensed works
that can be freely distributed in machine-readable form
accessible by the widest array of equipment including outdated
equipment. Many small donations ($1 to $5,000) are particularly
important to maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws


regulating charities and charitable donations in all 50 states of
the United States. Compliance requirements are not uniform
and it takes a considerable effort, much paperwork and many
fees to meet and keep up with these requirements. We do not
solicit donations in locations where we have not received written
confirmation of compliance. To SEND DONATIONS or determine
the status of compliance for any particular state visit
www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states


where we have not met the solicitation requirements, we know
of no prohibition against accepting unsolicited donations from
donors in such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot


make any statements concerning tax treatment of donations
received from outside the United States. U.S. laws alone swamp
our small staff.

Please check the Project Gutenberg web pages for current


donation methods and addresses. Donations are accepted in a
number of other ways including checks, online payments and
credit card donations. To donate, please visit:
www.gutenberg.org/donate.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could
be freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose
network of volunteer support.

Project Gutenberg™ eBooks are often created from several


printed editions, all of which are confirmed as not protected by
copyright in the U.S. unless a copyright notice is included. Thus,
we do not necessarily keep eBooks in compliance with any
particular paper edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg
Literary Archive Foundation, how to help produce our new
eBooks, and how to subscribe to our email newsletter to hear
about new eBooks.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookultra.com

You might also like