Book Image

Unity 4.x Cookbook

By : Matt Smith, Chico Queiroz
Book Image

Unity 4.x Cookbook

By: Matt Smith, Chico Queiroz

Overview of this book

<p>Covering the latest version (Unity 4) of this established game engine, the Unity 4.x Cookbook explores a wide range of 3D, animation, multimedia, and scripting game features.<br /><br />The power of Unity 4 is demonstrated through the 10 chapters covering many of this game engine’s features.<br /><br />"Unity 4.x Cookbook" helps you learn how to make the most of the powerful but easy-to-use Unity 4 game engine. <br /><br />Every Unity game developer is different. Some come from a multimedia background, some are new to game development, and some are transferring from other engines. Whatever your background, with the breadth and depth of topics covered you should find new features and techniques to enhance your next game.<br /><br />This book offers detailed, easy-to-follow, step-by-step recipes helping you master a wide range of Unity game features. <br /><br />Chapters are provided focusing on the basics; audio, cameras, images, materials and animations, GUIs, external resources and devices, directional and artificial intelligence character control, and performance optimization.<br /><br />"Unity 4.x Cookbook" provides a collection of ideas and resources that should offer new and useful techniques for every Unity game developer to enhance their next game.</p>
Table of Contents (18 chapters)
Unity 4.x Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Executing methods regularly but independent of frame rate with coroutines


Optimization principal 4: Call methods as few times as possible. While it is very simple to put logic into Update() and have it regularly executed each frame, we can improve the game performance by executing logic as occasionally as possible. So, if we can get away with only checking for some situations every five seconds, then great performance savings can be made to move that logic out of Update().

A coroutine is a function that can suspend its execution until a yield action has completed. One kind of yield action simply waits for a given number of seconds. In this recipe, we use coroutines and yield to show how a method can only be executed every five seconds. This could be useful for non-player characters to decide whether they should randomly "wake up", or perhaps choose a new location to start moving towards.

How to do it...

To implement methods at regular intervals independent of the frame rate, perform the follow...