Audio script

hi
how do i play a audio play in a script?
i wanna play footsteps…
my script:

var Footstep : audio.clip

function Update () {

if GetKey("w")
{
audio.Play(Footstep);
}
}

what is wrong?

is it a FPS character or 3rd person character?

If its a 3rdPerson you could add a collider to the foot and OnCollisionEnter play the audio clip As for FPS Im not sure. If you google it Im sure you’ll find everything you need :slight_smile: but here is a script or scripts that will allow you to have different sounds depending on what collider your walking on(metal surface, wood surface).

Foot.Js this goes on the actual foot create empty game object called Footsteps One for each foot with colliders etc… Images attached

var baseFootAudioVolume = 1.0; 
var soundEffectPitchRandomness = 0.05; 

function OnTriggerEnter (other : Collider) 
{ 
 	var collisionSoundEffect : CollisionSoundEffect = other.GetComponent(CollisionSoundEffect); 
 	
 	if (collisionSoundEffect) 
 	{ 
  		audio.clip = collisionSoundEffect.audioClip; 
  		audio.volume = collisionSoundEffect.volumeModifier * baseFootAudioVolume; 
 		audio.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness); 
  		audio.Play();  
 	} 
}

function Reset() 
{ 
 	rigidbody.isKinematic = true; 
 	collider.isTrigger = true; 
}

@script RequireComponent(AudioSource, SphereCollider, Rigidbody)

CollsionSound.js Then create a floor collider and attach this script

// Small script to hold a reference to an audioclip to play when the player hits me.

// This script is attached to game object making up your level. 
// The "Foot" script (which is attached to the player) looks for this script on whatever it touches.
// If it finds it, then it will play the sound when the foot comes in contact

var audioClip : AudioClip;
var volumeModifier = 1.0;

Now when you walk on the floor it will have a sound effect you can create a different floor with a different sound as well :wink: Hope it helps


621160--22116--$Picture 16.png

It is in first person perspective

have you dragged the audio to the newly created slot that was made when you made the script. It has to be linked to a audio file too

there is no created slot…

there is a compiling error…

var Footstep : AudioClip;

function Update () {

if GetKey("w") {

audio.PlayOneShot(Footstep);

}
}

it doesnt works:(

var Footstep : AudioClip;

function Update () {

if (GetKeyDown("w")) {

audio.PlayOneShot(Footstep);

}
}

Try that? :wink:

you have to attach the script to something. Create an empty and attach the script to it… When you click the empty you will see a new slot called somehing like “audio” and just drag the clip there

it doesnt works…
the console is saying:“BCE0005: Unknown identifier: ‘GetKeyDown’.”

It should be Input.GetKeyDown

You are my hero!
thats so typically for me that i forgot a simple Input. :frowning:
by the way: it works

oh. Forgot that. XD My mistake. :stuck_out_tongue: