The goal of this lab is to create an interactive scene where you can walk around and pick up various “home made” textured objects you have created with Blender 3D.
VIEN
folder in the Project panel. To do that, right-click on Assets
and select Create→Folder
and type VIEN
as the name. Under the VIEN folder, create three more folders: Scenes
, Scripts
and Props
. Plane
game object) and replace the Main Camera
with a FPSController
from the Characters
Assets Package. Make sure you can walk around your plane. Feel free to embellish this scene with other visuals (e.g. remove the skybox and add fog in Window→Lighting, sky color can be changed in the Camera
component of the FPSController→FirstPersonCharacter
game object). Save your scene in your “VIEN/Scenes” folder.Sphere Collider
. Add this collider component to your game object. In the Sphere Collider
check the Is Trigger
check box. You have now created an invisible sphere around your object, which generates a trigger event if something collides with it. You can control the radius of this sphere directly from the Radius
property in the Inspector panel.Scripts
folder (or create one under VIEN
if you haven't already). Right-click inside that folder and select Create→C# Script
. Name the new script Pickable
, double-click it to open the editor and add the following method to the class that appears: void OnTriggerEnter(Collider col) { if (col.gameObject.tag == "Player") { col.gameObject.SendMessage ("Pickup"); Destroy (gameObject); } }
Save the code (Ctrl-S) and back in the scene, select your object and in the Inspector press Add Component where you can use the search to find the Pickable
script. Add it.
FPSController
game object as the player, you have to give it a named tag. Select this game object in the scene and in the Inspector find the Tag
property near the top. Select Player
from a drop-down list of available tags. Now it is easy to “ask” this game object whether it's actually the player.Pickable
script, create another C# script in your script folder called Inventory
. Edit this script in the following way: public AudioClip pickupSound;
void Pickup() { AudioSource.PlayClipAtPoint (pickupSound, transform.position); }
Save the code (Ctrl-S) and back in the scene, select your FPSController
game object and in the Inspector press Add Component where you can use the search to find the Inventory
script. Add it.
Inventory
behavior to your player, you have to associate the pickupSound
variable of the Inventory
component in the Inspector panel with a sound asset. You can use whatever sound effect you like. You just have to make sure that the sound file is somewhere under the Unity Assets
folder so that you can browse to it. You can either fill in the value for the sound file by pressing the little target circle next to the edit box of the Pickup Sound
property, or you can drag a sound from the Project panel right into the edit box. After you have provided the sound, you should play the scene and make sure you hear it when the player runs into the object.VIEN
folder and create a new sub-folder called Prefabs
. Open that empty folder. Now drag the working instance of your object in the scene into this Prefabs
folder (you are essentially storing the complete configuration of your game object into a template kind of structure that Unity calls a Prefab). Change the name of your new prefab object in the Projects panel into PickableObject
or something like that (notice that the name also changes up in the scene). Now you can drag this prefab into your scene as many times as you want to make multple copies of it. If you wish to change any properties in all of them simultaneously, you can select the prefab instead of an object instance and use the Inspector to edit a property that then gets propegated to all instances of that prefab.Inventory
class that knows how many objects have been picked up. Display this number on the screen. To have a class write something onto the GUI, you override the void OnGUI()
method of that class (inherited from MonoBehavior
. This method gets called whenever the GUI gets updated by Unity. Here is an example of that method being used to write a sample number on the GUI:void OnGUI() { int number = 2; GUI.Label(new Rect(0,0,Screen.width, Screen.height),"Number:"+number.ToString()); }