- Home /
This question was
closed Aug 29, 2019 at 07:39 PM by
adovehv for the following reason:
The question is answered, right answer was accepted
Question by
adovehv · Aug 29, 2019 at 04:22 PM ·
playerenemysoundaudiosource
Play sound only once.
Hi! i have a enemy and i want to play his sound, i play his sound when the player is near him, but if the player not move, all time it called, like a Update(), how could I call the sound correctly? Thanks :)
if (target != initialPosition && distance < visionRadius)
{
sound.Play();
anim.SetBool("Emerge", true);
anim.SetBool("Hide", false);
Debug.DrawLine(transform.position, target, Color.blue);
}
else
{
anim.SetBool("Hide", true);
anim.SetBool("Emerge", false);
}
And here is my animator and its transitions
Comment
Best Answer
Answer by Hellium · Aug 29, 2019 at 05:29 PM
// class member
private bool inSight;
// ...
if (target != initialPosition && distance < visionRadius)
{
if(!inSight)
{
sound.Play();
anim.SetBool("Emerge", true);
anim.SetBool("Hide", false);
Debug.DrawLine(transform.position, target, Color.blue);
inSight = true;
}
}
else if(inSight)
{
anim.SetBool("Hide", true);
anim.SetBool("Emerge", false);
inSight = false;
}
Follow this Question
Related Questions
play sound after destroy the enemy by tag 1 Answer
can foe hear us? 1 Answer
Player damage stops working over time 0 Answers
Kill the Player? 1 Answer
How to stop enemy within certain distance of player? 2 Answers