Sunday, 15 April 2012

hey guys, heres the code iv done so far for the audio stuffs:

Sound Management:

//****************************************************************************************************
//VARIABLE DECLARATION
//****************************************************************************************************
public var levelNo: int = 1;
public var firstPlay: boolean = true;
private var levelManager: GameObject;
private var pc: GameObject;
private var cam: GameObject;
var pcZ: double;
var camZ: double;
var sounds: GameObject[];
var clipNo: int;
var soundName: String;
//****************************************************************************************************
//FIND NECESSARY GAME OBJECTS
//****************************************************************************************************
function Start()
{
    levelManager = GameObject.Find("Level Manager");
    pc = GameObject.Find("PC Prefab(Clone)");
    cam = GameObject.Find("PC Camera(Clone)");
}
//****************************************************************************************************
//AMBIENT MUSIC CODE FOR LEVELS 1, 2 AND 3
//****************************************************************************************************
function Update()
{   
    pcZ = pc.transform.position.z;
    camZ = cam.transform.position.z;

    if(pcZ > (camZ - 10) && firstPlay == true)
    {
        cleanUp();
        clipNo = (levelNo*2) - 2;
        firstPlay = false;
        Instantiate(sounds[clipNo], cam.transform.position, transform.rotation);
    }
    else if(pcZ <= (camZ - 10) && firstPlay == false)
    {
        cleanUp();
        firstPlay = true;
        clipNo = (levelNo*2) - 1;
        Instantiate(sounds[clipNo], cam.transform.position, transform.rotation);
    }
}
//****************************************************************************************************
//EVENT ACTIVATED SOUND
//****************************************************************************************************
function PlayPCSound(soundName: String)
{
    switch(soundName)
    {
        case "jump":
       
            break;
           
        default:
            levelManager.DisplayMessage("Sound Manager: ERROR! invalid sound clip name.");
            break;
    }
}
//****************************************************************************************************
//DELETE ALL EXISTING AMBIENT SOUNDS
//****************************************************************************************************
function cleanUp()
{
    var soundObj: GameObject;

    soundObj = GameObject.Find("Sound0(Clone)");
    Destroy(soundObj);
    soundObj = GameObject.Find("Sound1(Clone)");
    Destroy(soundObj);
    soundObj = GameObject.Find("Sound2(Clone)");
    Destroy(soundObj);
    soundObj = GameObject.Find("Sound3(Clone)");
    Destroy(soundObj);
    soundObj = GameObject.Find("Sound4(Clone)");
    Destroy(soundObj);
    soundObj = GameObject.Find("Sound5(Clone)");
    Destroy(soundObj);
}

Play Sound:

var clip: AudioClip;
var Cam: GameObject;

function Start()
{
    AudioSource.PlayClipAtPoint(clip, Cam.transform.position, 0.5);
   
}
var clip: AudioClip;
var Cam: GameObject;

function Start()
{
    AudioSource.PlayClipAtPoint(clip, Cam.transform.position, 0.3);
   
}

function End()
{
    Destroy(this);
}

No comments:

Post a Comment