- Home /
Playing a different sound after collecting an object(Slender Type sort of)
Hi,
I am trying to make different soundtracks play after for example (two chests are collected) it's sort of similar to Slender after the character picks up two papers. Here is what I've done so far(I'm using the slender guide version 2 to help me out)
pragma strict
script RequireComponent( AudioSource )
var papers : int = 0; var papersToWin : int = 8; var distanceToPaper : float = 2.5;
public var paperPickup : AudioClip; public var soundtrack1 : AudioClip; public var soundtrack2 : AudioClip; public var soundtrack3 : AudioClip; public var soundtrack4 : AudioClip;
var theEnemy : EnemyScript;
function Start() { Screen.lockCursor = true;
// find and store a reference to the enemy script (to reduce distance after each paper is collected)
if ( theEnemy == null )
{
theEnemy = GameObject.Find( "Enemy" ).GetComponent( EnemyScript );
}
}
function Update() { //if ( Input.GetMouseButtonUp(0) ) // use E in editor as LockCursor breaks with left mouseclick if ( Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.E) ) { //var ray = Camera.main.ScreenPointToRay( Input.mousePosition ); // always cast ray from center of screen var ray = Camera.main.ScreenPointToRay( Vector3( Screen.width 0.5, Screen.height 0.5, 0.0 ) ); var hit : RaycastHit; if ( Physics.Raycast( ray, hit, distanceToPaper ) ) { //if ( hit.collider.gameObject.tag == "Paper" ) if ( hit.collider.gameObject.name == "Chest" ) { papers += 1;
if( papers > 2)
{
audio.Play( soundtrack1);
}
else if (papers > 4)
{
audio.Play( soundtrack2);
}
else if(papers > 6)
{
audio.Play( soundtrack3 );
}
else if(papers > 7)
{
audio.Play( soundtrack4);
}
//Debug.Log( "A paper was picked up. Total papers = " + papers );
audio.PlayClipAtPoint( paperPickup, transform.position );
Destroy( hit.collider.gameObject );
// make enemy follow closer
theEnemy.ReduceDistance();
}
}
}
}
function OnGUI() { if ( papers < papersToWin ) { GUI.Box( Rect( (Screen.width * 0.5) - 60, 10, 120, 25 ), "" + papers.ToString() + " Chests" ); } else { GUI.Box( Rect( (Screen.width/2)-100, 10, 200, 35 ), "All Chests Collected!" ); // Application.LoadLevel( "sceneWin" ); } }
Any help would be very much appreciated.
The formatting on your code is messed up, can you fix it?
Your answer
![](https://koobas.hobune.stream/wayback/20220613091105im_/https://answers.unity.com/themes/thub/images/avi.jpg)