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 4. Interactions

In this chapter, we'll be looking at further interactions and dive into two of the most crucial elements of game development, namely, Collision Detection and Ray Casting.

To detect physical interactions between game objects, the most common method is to use a Collider component — an invisible net that surrounds an object's shape and is in charge of detecting collisions with other objects. The act of detecting and retrieving information from these collisions is known as collision detection.

Not only can we detect when two colliders interact, but we can also pre-empt a collision and perform many other useful tasks by utilizing a technique called Ray Casting, which draws a Ray — put simply, an invisible (non-rendered) vector line between two points in 3D space — which can also be used to detect an intersection with a game object's collider. Ray casting can also be used to retrieve lots of other useful information such as the length of the ray (therefore — distance), and...

Exploring collisions


When objects collide in any game engine, information about the collision event becomes available. By recording a variety of information upon the moment of impact, the game engine can respond in a realistic manner. For example, in a game involving physics, if an object falls to the ground from a height, then the engine needs to know which part of the object hit the ground first. With that information, it can correctly and realistically control the object's reaction to the impact.

Of course, Unity handles these kinds of collisions and stores the information on your behalf, and you only have to retrieve it in order to do something with it.

In the example of opening a door, we would need to detect collisions between the player character's collider and a collider on or near the door. It would make little sense to detect collisions elsewhere, as we would likely need to trigger the animation of the door when the player is near enough to walk through it, or to expect it to open...

Ray casting


While we can detect collisions between the player character's collider and a collider that fits the door object, a more appropriate method may be to check for when the player character is facing the door we are expecting to open and is within a certain distance of this door. This can be done by casting a ray forward from the player's forward direction and restricting its length. This means that when approaching the door, the player needn't walk right up to it — or bump into an extended collider — in order for it to be detected. It also ensures that the player cannot walk up to the door facing away from it and still open it — with ray casting they must be facing the door in order to use it, which makes sense.

In common usage, ray casting is done where collision detection is simply too imprecise to respond correctly. For example, reactions that need to occur with a frame-by-frame level of detail may occur too quickly for a collision to take place. In this instance, we need to preemptively...

Adding the outpost


Before we begin to use both collision detection and ray casting to open the door of our outpost, we'll need to introduce it to the scene. In Chapter 2, we set up the animation states for the model, and it will be those states that we'll be addressing when we write a script later in the chapter.

To begin, drag the outpost model from the Project panel to the Scene view and drop it anywhere — bear in mind you cannot position it when you drag-and-drop; this is done once you have dropped the model (that is, let go off the mouse).

Once the outpost is in the Scene, you'll notice its name has also appeared in the Hierarchy panel and that it has automatically become selected. Now you're ready to position and scale it!

Positioning

As your terrain design from Chapter 2 may be different to mine, select the Transform tool and position your outpost in a free area of land by dragging the axis handles in the scene.

Tip

Be careful when using the axis handles for positioning. Dragging the...

Opening the outpost


In this section, we will look at the two differing approaches for triggering the animation giving you an overview of the two techniques that will both become useful in many other game development situations. In the first approach, we'll use collision — detection a crucial concept to get to grips with as you begin to work on games in Unity. In the second approach, we'll implement a simple ray cast forward from the player.

Approach 1—Collision detection

To begin writing the script that will trigger the door-opening animation and thereby grant access to the outpost, we need to consider which object to write a script for.

In game development, it is often more efficient to write a single script for an object that will interact with many other objects, rather than writing many individual scripts that check for a single object. With this in mind, when writing scripts for a game such as this, we will write a script to be applied to the player character in order to check for...

Summary


In this chapter, we have explored two key methods for detecting interactions between objects in 3D games. Both ray casting and collision detection have many individual uses, and they are key skills that you should expect to reuse in your future use of Unity.

In the next chapter, we'll look into another method of collision detection while using colliders of objects set to trigger mode. This mode allows collision detection but removes the physical presence of an object, allowing — for example — the collection of objects without the player bumping into them. Try to think of triggers as collisions with no impact. While we could have taken this approach with the outpost door, it is important to learn the core concept of collision detection first, which is why we are learning in this particular order.

We will create a collection game in which the player must find four batteries in order to recharge the lock on the outpost door, and not allow entry to the outpost unless these batteries have...

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