AD41700 Computer Games Prof. Fabian Winkler Fall 2011 Shooting in Unity3D (continued) In this tutorial I would like to continue where we left off in the Shooting tutorial. Specifically I would like to introduce a script that detects if you have hit something with your bullet or not. I added two more cubes to the previous scene the red one called stand, the turquoise one called top_cube. Both new game objects have rigid body components added to them. The idea is that when one of the bullets is hitting them that they would fall to the floor. You can already try it out and see the physics engine is at work: Winkler, Scripting, Counters and GUI workshop, p. 1
Next we would like to detect the collision between the bullet and the red stand, so I wrote the following script ( collisionscript ) using Unity s built-in OnCollisionEnter function: I attach this script to the bullet prefab. When you shoot at the red stand and hit it you should see Hit! in the status bar at the bottom of the scene window. Let s trigger a sound this time when we hit the stand. I prepared a sound file called bang.aiff (remember Unity only likes uncompressed sound -.aiff or.wav or ogg/vobis. Import the sound file into your Project window: Asset > New Asset Next, select it and in the Inspector check off the 3D sound option and hit Apply. I change the collisionscript to include two lines that trigger the audio playback: var mysound : AudioClip; audio.playoneshot(mysound); Don t forget to drag and drop the sound asset onto the variable mysound in the Inspector after selecting the prefab, so the correct sound is assigned to the script. Finally, Component > Audio > AudioSource In the Inspector, choose bang as the Audio Clip and uncheck Play On Awake : Winkler, Scripting, Counters and GUI workshop, p. 2
Try it out and see what happens. Every time the cube bullet hits the red stand the bang sound is triggered. To only trigger it once we change the script slightly and introduce a variable that keeps track if the sound has already played: var mysound : AudioClip; var istriggered = false; if (istriggered == false) audio.playoneshot(mysound); istriggered = true; Housecleaning Now that we know how to create game objects dynamically at runtime it is a good idea to think about removing them eventually as well. With the scripting experience you have gained so far you can figure out different events that might remove the cube bullets from your scene (e.g. when they collide with another game object), in the following script ( destroy_bullets ), I use a timer that automatically removes (destroys) the game objects after a certain amount of time: Winkler, Scripting, Counters and GUI workshop, p. 3
var timeremaining = 3.0; function Update() timeremaining -= Time.deltaTime; if (timeremaining <= 0.0) Destroy(gameObject); Simply attach this script to the bullet prefab and watch the cubes disappear after 3 seconds. Hit Counter with Onscreen Text In the next step I would like to setup a counter that counts the number of hits and displays them as onscreen text. First I create a GUI text game object: Game Object > Create Other > GUI Text I name this new game object GUI_counter Then I create a script called GUItext_script which controls the text of the GUI and also the text color: var hitcounter = 0; var color : Color = Color(0.8, 0.7, 0.0, 1.0); // orange/fully opaque function Start() guitext.material.color = color; function Update() guitext.text ="Number of Hits: "+ hitcounter; This script is attached to the GUI_counter game object in the Hierarchy window (just drag and drop it onto GUI_counter Winkler, Scripting, Counters and GUI workshop, p. 4
Because I would like to update the hitcounter variable in the GUItext_script from within the collisionscript I need to add a couple of line of code to collisionscript : var mysound : AudioClip; var istriggered = false; var hitcounter = 0; private var otherscript : GUItext_script; function Awake() otherscript = GameObject.Find("GUI_counter").GetComponent(GUItext_script); if (istriggered == false) audio.playoneshot(mysound); otherscript.hitcounter++; istriggered = true; The Awake() function initializes the variable otherscript with a reference to the script GUItext_script which is attached to the GUI_counter game object. Winkler, Scripting, Counters and GUI workshop, p. 5
You can now hit the play button and start counting how many times you have hit the target: Winkler, Scripting, Counters and GUI workshop, p. 6