Tuesday, June 28, 2011

Play animation and sound when GUI button is pressed



I have a GUI button that I have created. When the button is pressed I would like a game object to play a certain animation and also play a sound.
var lever : AudioClip;
if (GUI.Button (Rect(510,150,80,20), "Lever"))
 {
 animation.Play ("lever1"); 
audio.PlayOneShot(lever);
 }
=====================================================================================================================================================================
// Plays the default animation
animation.Play();
For a specific animation, you can call the animation with play as well.

// Plays the walk animation - stops all other animations in the same layer
animation.Play("walk");
// Plays the walk animation - stops all other animations
animation.Play("walk", PlayMode.StopAll);
====================================================================================================================================================================

AudioSource.PlayOneShot  

function PlayOneShot (clip : AudioClip, volumeScale : float = 1.0F) : void

Description

Plays an AudioClip.

// Play impact audio clip when colliding with something
var impact : AudioClip;
function OnCollisionEnter () {
audio.PlayOneShot(impact);
}

No comments:

Post a Comment