Posted by & filed under Business, Content - Highlights and Reviews.

A guest post by Mike Geig, an experienced teacher and game developer, with a foot firmly in both camps who currently teaches game design and development at Stark State College and the Cleveland Institute of Art. Mike also works as a screencaster for Unity Technologies and is a member of Unity’s Learn department.

Unity’s graphical editor allows you to build all sorts of things in a very powerful and visual manner. There are times, though, where you may just want to build something algorithmic. You wouldn’t, for example, want to build a large wall of blocks by placing each one individually. Luckily, you can write scripts that make the process rather trivial, and it all boils down to the Instantiate() method and a little creativity.

This article assumes some basic knowledge of Unity such as creating scripts and game objects. If you need help with any of that, try the Game Development Essentials with Unity 4 LiveLessons video and the Learning Game Development with Unity video. And, check out my other post, Shooting Lasers in Unity to help master the use of Rays and Line Renderers in Unity.

The Method in Question

You can create objects in a scene using the Instantiate method in Unity. The syntax for the method looks like this:

You can see that all you have to do is tell the method what object to create and where to create it at, and the rest is handled for you. It is worth noting that since the objects are placed while the scene is running (and thus the script is running), they will disappear when the scene finishes execution. They will not stay permanently in the editor (a fix for that later in this post).

Starting Off Small

Let’s start off with the basics, by making a floor. That may seem easy to do without scripting, until you want to make a floor comprised of 100 different blocks. Sure you could place them all in the editor, but we’re programmers (let’s start acting like it). Here is a script that will build a floor with every odd row containing an offset:

This basic script simply creates a grid of blocks, alternating an offset of .5f every odd row. Besides a basic 2D grid iteration with the double FOR loops, the only other slightly complex part to this is using the x and z variables to create new Vector3’s for every block. Don’t worry if you don’t know about Quaternion.identity, it basically just means that the object will be positioned with no rotation and will align with the x, y, and z axes.

This script will give you something that looks like the following:

Cap1

Creating a Cylinder

Now let’s move onto something a little more complex. Say you wanted to build a silo or some other cylindrical object, and again you want to make it composed of many pieces. This time, even trying to make it procedurally can be a bit difficult. For starters, we would have to adapt the algorithm:

This works with a cylinder (that’s for circles) and then you would have to determine how much to rotate each block to make it right. This is where creativity comes in handy. Instead of trying to place each block correctly to form a cylinder, you can instead focus on simply creating vertical lines, and parent those lines to a rotating game object. This will easily allow you to create a cylinder. Let’s look at a script that will do that for us:

Let’s take a look at what this code is doing. First off, this should be placed on an object that is OK to move. Generally an empty game object works best.  Then you define exactly how many rows and columns this cylinder should have. Of course, if you go over 60 columns, you will need to adjust the rotation (going lower works fine).

Next, let’s do a basic 2D grid array. The difference is that as you build this grid, you will be rotating it. You achieve that effect by parenting every block created to the transform of whatever object this script is attached to.  Take a look at this line:

It allows you to save the object that was just created in a variable. That way you can parent it with this line:

With every completed vertical row, the parent object is rotated. And, the end result is a beautiful cylinder:

Cap2

Lock It In

There are many situations where building your level programmatically is insanely beneficial. Your only limitation is your creativity (an understanding of math doesn’t hurt). Many times, however, you want the objects you create to be permanently in your scene. What good is a floor, for example, if you can’t place items on it, or even see it, in edit mode? Luckily, you can easily make your programmed creations a part of your scene with the “ExecuteInEditMode” attribute. Do this by placing the attributes in square brackets right above your class name, like so:

The Start() method will automatically run inside the editor. The result is that whatever manipulations the script does will be permanent. One word of warning though, remove the attribute after the items are created in your scene. You would hate to keep making duplicates and not notice.

Now go forth and programmatically generate any scene that you desire!

Look below for some great Unity resources from Safari Books Online.

Not a subscriber? Sign up for a free trial.

Safari Books Online has the content you need

Learning Game Development with Unity 3D introduces you to the fundamentals of 3D game design with the Unity engine. This tutorial is designed with the absolute beginner in mind; no prior experience with Unity is required.
Advanced Unity 3D Game Development teaches you how to use some of the advanced features available to you within the Unity 4 game engine. This course is designed for the experienced Unity developer. You should have a working understanding of the Unity 4 engine and features before taking this tutorial.
Learning C# by Developing Games with Unity 3D Beginner’s Guide starts by explaining in simple terms the three concepts you need for writing C# code and scripts: 1) variables to hold information; 2) methods (functions) to use the information; and 3) Dot Syntax to communicate the information where it’s needed. The book builds on these concepts to open up the world of C# coding and Unity scripting. You will use this new power to access the features provided in Unity’s Scripting Reference.
Game Development Essentials with Unity 4 LiveLessons demonstrates the power and versatility of the Unity 4 engine and helps you leverage this engine in your own game development endeavors. Geig covers the Unity interface, concepts of 2D and 3D game development, building terrain for your games, as well as developing game objects that interact through collision. You will also learn to work with scripts and manipulate objects through code. And for those of you who want to develop for mobile devices, you will find coverage here as well. Finally, the course ends with a lesson on how to construct your own game with the Unity 4 game engine.
Game Development for iOS with Unity3D takes you through the complete process of Unity iOS game development. A game developer for over 12 years, the author presents production-proven techniques and valuable tips and tricks needed to plan, build, test, and launch games for the iPhone, iPod, and iPad. He walks you through all the necessary procedures, including how to publish your game to the App Store.

About the author

Me-1131-Edit Mike Geig is both an experienced teacher and game developer, with a foot firmly in both camps. He currently teaches game design and development at Stark State College and the Cleveland Institute of Art. Mike also works as a screencaster for Unity Technologies and is a member of Unity’s Learn department. His Pearson video, Game Development Essentials with Unity 4 LiveLessons is a key title on Unity. Mike was once set on fire and has over a million “likes” on Facebook and can be reached at @mikegeig.

Tags: Geometry, Instantiate, Procedurally, Scripts, Unity,

Comments are closed.