- Home /
Sound Problem !
So there is my script :
var distance;
var target : Transform;
var lookAtDistance = 15.0;
var attackRange = 10.0;
var moveSpeed = 5.0;
var damping = 6.0;
var gameobject : GameObject;
var auidioclip : AudioClip;
private var isItAttacking = false;
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
isItAttacking = false;
audio.PlayOneShot(audioclip);
lookAt ();
}
if(distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if(distance < attackRange)
{
attack ();
}
if(isItAttacking)
{
Destroy(gameObject);
}
}
function lookAt () { var rotation = Quaternion.LookRotation(target.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping); }
function attack () { isItAttacking = true; Destroy(gameObject);
transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
}
The problem is when i go close to the enemy , the sound start repeating many times ... But i want to hear the sound only 1 time in my game. I mean that i want to hear the sound normally once and then i want it to mute for ever. cant anyone please help me ?
Answer by AlucardJay · Sep 09, 2012 at 06:55 PM
this is where your audio clip is playing every frame :
if(distance < lookAtDistance)
{
isItAttacking = false;
audio.PlayOneShot(audioclip);
lookAt ();
}
you need to set up a counter or boolean that states if the sound is still playing or not. Here is some untested code (to add to, and change your script with) :
var isAudioClipPlaying : boolean = false;
// ....
if(distance < lookAtDistance)
{
isItAttacking = false;
// audio.PlayOneShot(audioclip);
if (!isAudioClipPlaying) {
DistanceCloseAudio();
}
lookAt ();
}
// ....
function DistanceCloseAudio()
{
isAudioClipPlaying = true;
audio.PlayOneShot( audioclip );
yield WaitForSeconds( audioclip.length );
isAudioClipPlaying = false;
}
also your variable name in the question is : var auidioclip : AudioClip; ?? you meant : var audioclip : AudioClip; =]
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Sound script problem. 0 Answers
Having problems when using triggers 1 Answer
Prefab is not working anymore after opening my project. 3 Answers
Joystick 10th axis code problem 1 Answer