Hello! I am having some difficulty on making the “DoorCloseSound” play after I close my door. When the door opens or closes, it will only play the “DoorOpenSound”. I followed a tutorial on YouTube and had to modify it a little bit as the scripting library was in an older version of Unity. If you could take the time to read through this and help me out that’d be great! Here’s what I got thus far:
using UnityEngine;
using System.Collections;
public class DoorScript : MonoBehaviour {
public bool open = false;
public float doorOpenAngle = -90f;
public float doorCloseAngle = 0f;
public float smooth = 2f;
public AudioClip DoorOpenSound;
public AudioClip DoorCloseSound;
void Start ()
{
}
public void ChangeDoorState()
{
open = !open;
GetComponent<AudioSource> ().Play();
}
void Update ()
{
if (open)
{
Quaternion targetRotation = Quaternion.Euler (0, doorOpenAngle, 0);
transform.localRotation = Quaternion.Slerp (transform.localRotation, targetRotation, smooth * Time.deltaTime);
}
else
{
Quaternion targetRotation2 = Quaternion.Euler (0, doorCloseAngle, 0);
transform.localRotation = Quaternion.Slerp (transform.localRotation, targetRotation2, smooth * Time.deltaTime);
}
}
}
I’m assuming in the “public void ChangeDoorState” I would have to make it
public void ChangeDoorState()
if
{
open = !open;
GetComponent<AudioSource> ().Play();
}
else
{
GetComponet<AudioSource>().Play();
}
But then of course I don’t have a specific audio for each statement, and also Unity says it’s not allowed haha. Thanks again!
I’ve tried that as well before actually posting, but it still only played the open sound. However, I only had this:
public void ChangeDoorState()
{
open = !open;
GetComponent<AudioSource> ().PlayOneShot();
}
and tried these as well with no avail…
public void ChangeDoorState()
{
open = !open;
GetComponent<AudioSource> ().PlayOneShot(DoorOpenSound);
Get Componet<AudioSource>().PlayOneShot(DoorCloseSound);
}
public void ChangeDoorState()
if
{
open = !open;
GetComponent<AudioSource> ().PlayOneShot(DoorOpenSound);
}
else
{
Get Componet<AudioSource>().PlayOneShot(DoorCloseSound);
}
public void ChangeDoorState()
{
open = !open;
GetComponent<AudioSource> ().PlayOneShot(DoorOpenSound);
open = false
Get Componet<AudioSource>().PlayOneShot(DoorCloseSound);
}
I also made sure that the correct audio clips are in the AudioClip, and I also have two audio sources, one with the close sound and one with the open sound assigned to the door.
Okay, well if you have 2 audio sources, the simplest way is just create 2 variables:
// drag and drop these 2 in the inspector!
public AudioSource openAudioSource;
public AudioSource closeAudioSource;
// later, when opening/closing..:
open = !open;
if(open) openAudioSource.Play();
else closeAudioSource.Play();
Side note, not really relevent if you get this working… but your “if” statement in some examples you wrote were invalid.
You don’t need two audiosources, you’ll only ever be playing one of those sounds at a given time.
public AudioClip DoorOpenSound;
public AudioClip DoorCloseSound;
public AudioSource AudSource;
...
void Start() {
//Cache the audiosource, don't use GetComponent more than once if you can help it
AudSource = GetComponent < AudioSource > ();
}
...
public void ChangeDoorState() {
if (AudSource.isPlaying) {
//Stop the audiosource if it's already playing
AudSource.Stop();
}
if (open) {
//Assign DoorOpenSound clip
AudSource.clip = DoorOpenSound;
}
else {
//Assign DoorCloseSound clip
AudSource.clip = DoorCloseSound;
}
open = !open;
AudSource.Play();
}
That idea’s a little better. Honestly, I was just going with what he had, but clips would be nicer.
I think I would move the “open = !open” higher up, though.
It looks to me like you’re unsure how if statements work, here’s how I’d do it based on your first code snippet.
public void ChangeDoorState()
{
open = !open;
if( open ) // we have just changed the state to open == true so play open sound
{
GetComponent<AudioSource> ().PlayOneShot(DoorOpenSound);
}
else // we have just changed the state to open == false so play close sound
{
GetComponent<AudioSource>().PlayOneShot(DoorCloseSound);
}
}
Thanks a lot guys for your help, it’s really appreciated! Sorry for the late response too, currently doing this in between my college classes or when I have free time. I’ll be sure to try these out. Again, thank you very much for the help!
Great news! Anthony went ahead and tried your way and it worked perfectly. In response to your statement, I am unsure how to properly word them but I do know how they work. I’ve tried learning little by little from this game I’m creating and I definitely know a lot more than what I did when I first started. But definitely not enough to do it on my own sadly. Hopefully I’ll be able to get to the point where I can crate a script without a tutorial some day haha! Thanks again!