Hopefully I can explain this. I have an animation of a door opening and closing. When I mouseclick it, the door will open and play the sound of the door opening. But the sound will repeat itself when I click the door again (which is already open). So I put an isPlayed statement in the code. That works.
But the thing is, when I close the door and open it again, the sound of the door opening won’t play (because of the isPlayed-statement). How can I make it work that the you can hear the sound once when opening the door and to keep it hearing everytime you close and open the door.
This is my code so far. Thanks for helping me out!
using UnityEngine;
using System.Collections;
public class SoundDeur : MonoBehaviour
{
public Animator anim;
private bool deurOpened = false;
public AudioClip soundFile;
AudioSource mySound;
public bool alreadyPlayed = false;
private bool isPlayed;
void Awake()
{
mySound = GetComponent<AudioSource>();
isPlayed = true;
}
void OnMouseDown()
{
if (!deurOpened)
{
anim.SetTrigger("deuropen");
if (isPlayed)
{
mySound.PlayOneShot(soundFile, 0.8f);
isPlayed = false;
}
}
}
}