- Home /
Music Wont Stop From Page Script+ Extra Problom
This code is from majestics slender kit.... i have edited it to play music just like the slendermans shadow games but for some reason the music wont stop on my code???? i also have a problom where the audio only plays where the page was picked up... why is my code doing this???
#pragma strict
@script RequireComponent( AudioSource )
var pages : int = 0;
var pagesToWin : int = 8;
var distanceToPage : float = 2.5;
public var pagePickup : AudioClip;
var theEnemy : EnemyScript;
public var FirstPage : AudioClip;
public var thirdpage : AudioClip;
public var FifthPage : AudioClip;
public var SeventhPage : AudioClip;
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, distanceToPage ) )
{
//if ( hit.collider.gameObject.tag == "Paper" )
if ( hit.collider.gameObject.name == "Page" )
{
pages += 1;
//Debug.Log( "A Page was Collected. Total papers = " + papers );
audio.PlayClipAtPoint( pagePickup, transform.position );
Destroy( hit.collider.gameObject );
}
if ( pages==1 )
audio.PlayClipAtPoint( FirstPage, Camera.main.transform.position);}
{
}
if ( pages==3 )
{
audio.Stop();
audio.PlayClipAtPoint( thirdpage, Camera.main.transform.position);
}
if ( pages==5 )
{
audio.Stop();
audio.PlayClipAtPoint( FifthPage, Camera.main.transform.position);
}
if ( pages==7 )
{
audio.Stop();
audio.PlayClipAtPoint( SeventhPage, Camera.main.transform.position);
}
// make enemy follow closer
theEnemy.ReduceDistance();
}
}
function OnGUI()
{
if ( pages < pagesToWin )
{
GUI.Box( Rect( (Screen.width * 0.5) - 60, 10, 120, 25 ), "" + pages.ToString() + " Pages" );
}
else
{
GUI.Box( Rect( (Screen.width/2)-100, 10, 200, 35 ), "All Pages Collected!" );
// Application.LoadLevel( "sceneWin" );
}
}
Answer by Mill0698 · Aug 08, 2013 at 02:29 PM
There are two common methods for triggering sounds from script.
The first common method to achieve this would be to not have an audio clip variable at all, but instead attach an AudioSource component to your GameObject (or to the pickup object), and drag the "pickup" audio clip reference into that audio source component.
You could then trigger the sound by calling
audio.Play(); from a script on the same object. Or, if you placed the AudioSource on the pickup object, you could call it from your player object when it's collected by using the "otherObject" variable in your OnTriggerEnter function, like this:
otherObject.audio.Play(); However, there is another way to play sounds which doesn't require an AudioSource component to be attached. If your script has a reference to the audioClip to play (as it does currently), a simple way to trigger the sound is to use PlayClipAtPoint.
You could place this code in your OnTriggerEnter function, near the line where the pickup is destroyed:
AudioSource.PlayClipAtPoint(pickup, transform.position); This will play the sound at the position of the player object (the object that this script is placed on) in the 3D world. Depending on your situation you may want to change this to:
AudioSource.PlayClipAtPoint(pickup, collider.transform.position); (to play the sound at the location of the other object)
or
AudioSource.PlayClipAtPoint(pickup, Camera.main.transform.position); (to play the sound at the camera's location, so that it doesn't sound as though it's coming from any particular location in the 3d world)
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
What happened to the Slender Guide by alucardj 1 Answer
Playing audio when a button is clicked? 1 Answer
Audio Wont Play In Page Script 0 Answers
How to add Elements? Multiple ones!????? Help PLease 1 Answer