Interactivity in Virtual Worlds: Using Triggers in Jibe and Unity3d

The physical world around us is responsive to our presence.

The same experience can be recreated in virtual worlds.

You just have to learn a few new tricks.

Those of us designing virtual worlds want to make them as engaging and interesting as possible.  There are many different ways to accomplish this goal.  You can start by creating a visually interesting space.  Make it beautiful to look at.  Fill it with things that move and look alive.  Encourage exploration by rewarding people with fun things to discover.  And never forget the power of sounds.

All of these methods will help you design a virtual space that is engaging to visitors.  But at some point you’ll realize that you want to build a world that is responsive to the people who are exploring it.

Looking and listening is fun.  Interactivity is even better.

Interactivity in Jibe and Unity

Jibe is a multiuser virtual world platform based on Unity3d.  I’ve been spending the past month learning the basics of how to create scenes in Unity and then deploy them as multiuser worlds using Jibe.  Whenever I learn something new, I like to write about it and share what I’ve learned.  You really don’t need to be an expert at Unity or programming or 3d modeling to do a lot of powerful things with Jibe.  You just need to have a curious mind and some imagination.

I’ve been experimenting with what I’ve learned in my own Jibe world, doing my best to make things interesting and engaging.  And I recently reached the point where I wanted to add some basic interactivity to my world.

I wanted to detect when an avatar in Jibe approached something and then make something happen as a result.

And fortunately, there’s a simple way to do that in Unity.

Building a simple Trigger in Unity

Triggers are a fundamental tool in Unity.  There’s a lot of detailed scripting information about how they work, but let’s try something basic.

Tutorial: How to make a rabbit that squeaks when you get close to him.

 – Step 1:  Get a rabbit.

If you lack the 3d modeling skills to build a rabbit from scratch (as I do), you’ll have to find a preexisting model.  Or you could use any 3d model you like for this tutorial.  I happen to like rabbits, so I’m going to use a rabbit as an example.

A great place to find preexisting models is the Unity Asset Store.  You can open it up from within the Unity editor and buy a nice rabbit model for just $10.

Here is how you access the Unity Asset Store from within the Unity editor.

Here is a nice rabbit for sale!

Once you buy something in the Unity Asset Store, it is automatically downloaded and added to your Project window in the Unity editor.  Drag the rabbit model out of your Project window and place it on the ground in your Unity scene.

Your rabbit sitting in your Unity scene.

You’ll want avatars to physically bump into the rabbit when they walk up to him.  So add a mesh collider to the rabbit model.  Otherwise, he’d be a non-physical phantom bunny!

Here is how you add a mesh collider to an object.

You can now see that the rabbit has a mesh collider.

– Step 2: Get a squeaking sound.

The Freesound Project is a great place to find tons of free sound samples.  Download a squeaking sound, rename it “squeak” and then drag it into your Project window in the Unity editor.  We’ll do something with this sound file later.

– Step 3:  Make a sphere that defines a “Trigger Zone.”

When someone gets close to your rabbit, you want them to hear the squeak sound.  You can do this in Unity by creating any object and setting its collider to be a Trigger.  So create a simple sphere as a new GameObject in your Unity scene.

Creating a sphere object in your Unity scene.

Rabbit and Sphere.

Open the Sphere’s settings in your Inspector and made sure the “Is Trigger” checkbox is selected.  This means that the sphere will now be a non-physical phantom object and will generate a Trigger event whenever an avatar walks into it.  The boundaries of the Sphere define the “Trigger Zone.”

Your Sphere is now ready to be a Trigger.

– Step 4:  Write a script that plays a sound file when someone’s avatar walks into the sphere.

You’ve got a sphere that defines a Trigger Zone.  Now you need the trigger to actually cause something to happen.  You can do that by writing a simple script that will detect when an avatar enters the Trigger Zone and will then play the sound file.

Create a new script in the Unity editor and call it “make a squeak.”

How to create a new script in the Unity editor.

Your script in the Unity editor.

Here’s the code for the script:

var sound1 : AudioClip;

function OnTriggerEnter (){
audio.clip = sound1;
audio.Play ();
}

function Update () {
}

Once you’ve saved the script, drag the script file from your Project window onto the Sphere.  And drag the “squeak” sound file onto the Sphere as well.

The squeak script and sound file are now in your Sphere. But something is missing!

See that blue area in the Inspector window?  You need to tell the “make a squeak” script what sound file to use.  Just click on the little circle all the way on the right and choose your “squeak” sound file from the list.  Then the script will know what audio clip to play.

Good to go!

– Step 5:  Position your sphere so it surrounds the rabbit.

Move and scale your sphere so that it surrounds the rabbit.  That way you can use the size of the sphere to set a specific distance from the rabbit before the squeak sound is triggered.

Your rabbit is now inside the sphere.

– Step 6:  Make the sphere invisible.

You don’t want people to actually see the sphere, so make it invisible by unchecking the box next to “Mesh Renderer” in the Inspector window.

Disable Mesh Renderer so people will not see the sphere.

The sphere is still there. But now nobody can see it.

– Congratulations!  You’re done!

There’s a whole lot more you can do with Triggers.  OnTriggerEnter functions can let you initiate all kinds of things, like transforming objects, changing variables and starting animations.  Walk up to a door, and it automatically opens.  Walk up to a bird, and it flies away.  Walk up to a dragon, and it roars and breathes fire!

Never underestimate the power of interactive elements in a virtual world.  Even a simple example like this will add to the immersiveness of your environment, making it a much more interesting and rewarding place to explore.

If you’d like to meet other people interested in building multiuser virtual worlds using Jibe and Unity3d, please join our Jibe and Unity3d Discussion Group.

And if you’d want to visit my own squeaking rabbits, you’ll find them in my Jibe world.

4 thoughts on “Interactivity in Virtual Worlds: Using Triggers in Jibe and Unity3d

  1. Great tutorial John, thanks!

    I’ve been messing with audio, and the triggering thereof, a fair bit for my own project and I just wanted to highlight a couple of things from the above that you may not have known. Firstly, you can disable the mesh renderer to hide the sphere as you have done above, but if the sphere never needs to be visible you can remove the Mesh renderer component altogether (click the little gear wheel top right and choose Remove Component). Similarly, if you’re not going to render it then you don’t actually need the mesh! Again use remove Component. This then just leaves you with the sphere collider which is the bit you’re using as your actual trigger. It doesn’t save a lot, but if you had a scene with a great many such rabbits then all those saved sphere meshes would add up!

    Then, in the script, Unity allows you to take certain shortcuts. If, as is the case with your rabbit, you only have one audioclip then you don’t need to specify it in the script. Unity will know that the audioclip attached to the object is the one it sound play, so the script above could be written simply as:

    function OnTriggerEnter(){
    audio.Play();
    }

    and it would do the same thing. You will find, however, that the sound will play as soon as the scene starts regardless of whether anything has triggered the collider. If that’s a problem (as it was for me) then just add a standard Start() function to halt it:

    function Start(){
    audio.Stop();
    }

  2. Pingback: The Riddle of the Sphinx: Using Google Warehouse objects in Jibe and Unity3d | Be Cunning and Full of Tricks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s