Game Development Reference
In-Depth Information
//Create a new key at the start position by cloning from our
prefab
Instantiate(key, new Vector3(key.transform.position.x, key.
transform.position.y, 0.0), key.transform.rotation);
//Hide restart button
guiTexture.enabled = false;
}
@scriptRequireComponent(AudioSource)
5. Then, we go back to our
CharacterController_2D.js
to add sound code for
playing the sound effect and a
restart
buton. Add these parameters to the top of
this class:
private var restartButton : GUITexture;
public var doorOpenSound : AudioClip;
public var getKeySound : AudioClip;
public var jumpSound : AudioClip;
6. Add code to get the
restartButton
from our game scene; put this code in the
Start()
funcion:
//Get restartButton from the Game Scene
restartButton = GameObject.FindWithTag("RestartButton").
guiTexture;
//make restart Button disabled
restartButton.enabled = false;
7. Add a jump sound, inside the
Update()
funcion and inside
if (Input.
GetButton("Jump")) {}
as follows:
if (Input.GetButton("Jump")) { //Jump
b_isJumping = true;
//Then make it Jump
audio.volume = 0.3;
audio.PlayOneShot(jumpSound);
loopSprites[0].resetFrame();
loopSprites[1].resetFrame();
rigidbody.velocity = new Vector3(rigidbody.velocity.x,
-Physics.gravity.y, 0);
}
8. Next, we add the
getKey
sound,
doorOpen
sound, and enable our
restartButton
. Go to
OnTriggerEnter()
and update the code. First, inside
if
(hit.collider.tag == "Key") {}
add the highlighted code:
if (hit.collider.tag == "Key") {
if (!b_hasKey) {
//We hit our Key