Reader small image

You're reading from  Unity Game Development Essentials

Product typeBook
Published inOct 2009
Reading LevelIntermediate
PublisherPackt
ISBN-139781847198181
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Will Goldstone
Will Goldstone
author image
Will Goldstone

Will Goldstone is a longstanding member of the Unity community and works for Unity Technologies as a Technical Support Associate, handling educational content and developer support. With an MA in Creative Education, and many years experience as a lecturer in higher education, Will wrote the first ever Unity book, the original Unity Game Development Essentials, and also created the first ever video tutorials for the package. Through his sites http://www.unity3dstudent.com and http://learnunity3d.com Will helps to introduce new users to the growing community of developers discovering Unity every day.
Read more about Will Goldstone

Right arrow

Chapter 6. Instantiation and Rigid Bodies

In this chapter, we'll be exploring two crucial concepts in 3D game design. In the first half, we'll look at the concept of Instantiation — the process of creating objects during runtime. We will then explore a practical example of instantiation, as we learn about the use of Rigid Body physics.

When you first begin to build game scenes, you'll realize that not all of the objects required within any given scene would be present at the start of the the game. This is true of a wide variety of game genres like puzzle games, such as Tetris. Puzzle pieces of random shapes are created or instantiated at the top of the screen at set intervals because all of them cannot be stored at the top of the screen infinitely.

Now take our island exploration game as another example. In this chapter, we'll be taking a look at rigid body physics by creating a method for our player character to play a simple coconut shy game, but the coconuts that will be thrown will not...

Introducing instantiation


In this section, we will learn how to spawn and duplicate objects while the game is running. This is a concept that is used in many games to create projectiles, collectable objects, and even characters, such as enemies.

In concept

Instantiation is simply a method of creating (also referred to as spawning) objects from a template (a prefab in Unity terms) during runtime. It can also be used to duplicate existing game objects already in the scene.

The approach when using instantiation will usually take this form:

  • Create the object that you wish to instantiate in your scene, and add components as necessary

  • Create a new prefab in your project, and drop the object you have been working on into that prefab

  • Delete the original object from the scene so that it is only stored as a prefab asset

  • Write a script that involves the Instantiate() command, attach it to an active game object, and set the prefab you created as the object that the Instantiate() command creates

In code

At its...

Introducing rigid bodies


Physics engines give games a means of simulating realism in physical terms, and they are a feature in almost all game engines either natively or as a plugin. Unity utilizes the Nvidia PhysX physics engine, a precise modern physics engine that is used in many commercial games in the industry. Having a physics engine means that not only physical reactions, such as weight and gravity are possible, but realistic responses to friction, torque, and mass-based impact, are also possible.

Forces

The influence of the physics engine on objects is known as force, and forces can be applied in a variety of ways through components or scripting. In order to apply physics forces, an object must be what is known as a rigid body object.

The Rigidbody component

In order to invoke the physics engine in Unity, you must give an object a rigidbody component. This simply tells the engine to apply the physics engine to a particular object you need not apply it to an entire scene. It simply works...

Making the minigame


To put into practice what we have just looked at, we'll create a coconut shy game that ties into our access to the outpost. By playing the game, the player will be rewarded with the final battery they require to charge the outpost door.

As we have already set up the battery charge element of the game, we simply need to remove one of the batteries from the existing scene, leaving the player with one less.

Select one of the objects called battery in the Hierarchy panel, and then remove it with Command + Backspace (Mac) or Delete (PC).

Creating the coconut prefab

Now that we have learnt about instantiation, we'll begin our minigame by creating the object to be thrown, that is, the coconut.

Go to Game Object | Create Other | Sphere.

This creates a new sphere primitive object in the scene. While it will not be created directly in front of the editor viewport, you can easily zoom to it by hovering your cursor over the Scene view and pressing F (focus) on the keyboard. Rename...

Summary


In this chapter, we have covered various topics that you will find crucial when creating any game scenario. We have looked at implementing rigid body objects that use the physics engine. This is something you'll likely expand upon in many other game scenarios while working with Unity. We also explored the concept of instantiation, something that is very important to get to grips with, as it means you can create or clone any prefab asset or game object during runtime — a very useful tool in your game-designing arsenal.

We also gave the player further feedback by reusing our TextHint GUI object made in Chapter 5 and worked across scripts in order to send the information to this object.

These are concepts you will continue to use in the rest of this book and in your future Unity projects. In the next chapter, we'll take a break from coding and take a look at more of Unity's aesthetic effects. We'll explore the use of particle systems to create a fire outside the outpost cabin — giving...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity Game Development Essentials
Published in: Oct 2009Publisher: PacktISBN-13: 9781847198181
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.
undefined
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 €14.99/month. Cancel anytime

Author (1)

author image
Will Goldstone

Will Goldstone is a longstanding member of the Unity community and works for Unity Technologies as a Technical Support Associate, handling educational content and developer support. With an MA in Creative Education, and many years experience as a lecturer in higher education, Will wrote the first ever Unity book, the original Unity Game Development Essentials, and also created the first ever video tutorials for the package. Through his sites http://www.unity3dstudent.com and http://learnunity3d.com Will helps to introduce new users to the growing community of developers discovering Unity every day.
Read more about Will Goldstone