Hello! I am having some difficulty on making the a door close sound play after I close my door.
Currently, the sound is played when my player leaves the door trigger but also when he accidentally knocks on one door or when he steps back in the trigger of another door
I think the sound should only be played when my door animation ends but I don’t know how to do it
If you could take the time to read through this and help me out that’d be great!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OpenableDoor : MonoBehaviour
{
private Animator _animator;
private bool _isInsideTrigger = false;
private bool ouvrable = false;
//essayer bruit de claquement de porte
public AudioClip claquement;
private AudioSource source;
// Use this for initialization
void Start () {
// le clip audio doit être importé dans le component Audio source du trigger auquel est associé ce script
source = this.GetComponent<AudioSource>();
_animator = transform.Find("Door").GetComponent<Animator>();
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
_isInsideTrigger = true;
ouvrable = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
_isInsideTrigger = false;
_animator.SetBool("open", false);
ouvrable = false;
//ici bruit de fermeture de porte, le bruit apparait à 2 secondes dans le clip
source.PlayOneShot(claquement, 0.9f);
}
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.E) && ouvrable==true)
{
_animator.SetBool("open", true);
}
}
void OnGUI()
{
if (_isInsideTrigger)
{
GUI.Label(new Rect(Screen.width / 2 - 75, Screen.height - 100, 150, 30), "Press 'E' to open the door");
}
}
}