The Lost Office presents you with an array of challenges. The best candidates can solve all of them within 60 seconds. Those who don't, well, let's just say those candidates didn't "make the cut." Be warned, Deja Vu may be the case here as each challenge differs only slightly from the last.

Post tutorial Report RSS Using tags to handle interactions in Unity

With the myriad of different types of objects found in The Lost Office (in development) we needed a way to classify them, yet make them all interact-able.

Posted by on - Intermediate Client Side Coding

Wrangling up all those different possibilities with Tags in Unity.

by Sub-Zero Chuck

With the myriad of different types of objects found in The Lost Office (in development) we needed a way to classify them, yet make them all interact-able. Some objects might be containers that can open or close, some things might be able to be picked up. Some objects might be furniture for sitting/sleeping. Some things might be consumables.

Yet, we only have one way to interact with all these different objects... our mouse, and it only has three buttons.

Fishing for Raycast hits:

The Lost Office is a first person adventure. You'll need to interact with all sorts of objects, but the game needs to know which one you are interested in.

To solve this, we put a reticule in the center of the canvas, and we cay rays out from that point. Since the canvas is always in front of the camera, we can safely use this as a reference point to select an object for interaction.

We caught one, now what?

So, our raycast hit a collider. Great! We now have a gameObject to work with. But what do we do? That really depends on what that object is. You can open a door, but you can't pick it up. I wouldn't suggest trying to eat a door, either. But how will we decide what the player meant when they clicked on the door?

What if we knew the door, was indeed a door. We could write a huge database of different interactions you could have in the main Inspector script, but since that script is watching Input, it runds on Update, and you'd be parsing a lot of different options everytime.

Things should be uniform to some extent. And when they are, the function GetComponent() serves for a generic way to call on the object.

We elected to attach individual animations along with individual scripts to each of the objects. We can call upon the animation simply by using GetComponent() and that Animation can handle any scripts attached.

Is it really this simple?

No quite. Sometimes, we need to do a little more than just play an animation, butwe still need some general guidelines to keep things organized. This is where tagging comes in. Based on a preset tag, we know the general type of object we are dealing with, a door, a food, a container, a tool, etc, based on its tag. If we know which tag, then we can call on a known script component which will be attached to all like objects. A gameobject with the tag "Tool" will have a "UseWith(gameObject)" component, "Food" = EatObject(), so on, and so forth.

Boy that's rich!

Exactly, but also manageable. We may have as many as 100 different object to interact with, and now, we have a class system in which we can group them. The result, we can start interactions based on general tags, but still have unique results for each interaction.

If you want more details, watch this basic tutorial we whipped up.

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: