Search Unity

Adding a Close sound to Door Script (help)

Discussion in 'Scripting' started by wrm186, Dec 6, 2017.

  1. wrm186

    wrm186

    Joined:
    Apr 4, 2010
    Posts:
    661
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DoorScript : MonoBehaviour {
    5.  
    6.     public bool open = false;
    7.     public float doorOpenAngle = -90f;
    8.     public float doorCloseAngle = 0f;
    9.     public float smooth = 2f;
    10.     public AudioClip DoorOpenSound;
    11.     public AudioClip DoorCloseSound;
    12.  
    13.     void Start ()
    14.     {
    15.    
    16.     }
    17.    
    18.  
    19.     public void ChangeDoorState()
    20.         {
    21.         open = !open;
    22.         GetComponent<AudioSource> ().Play();
    23.         }
    24.        
    25.     void Update ()
    26.     {
    27.         if (open)
    28.         {
    29.             Quaternion targetRotation = Quaternion.Euler (0, doorOpenAngle, 0);
    30.             transform.localRotation = Quaternion.Slerp (transform.localRotation, targetRotation, smooth * Time.deltaTime);
    31.         }
    32.         else
    33.         {
    34.             Quaternion targetRotation2 = Quaternion.Euler (0, doorCloseAngle, 0);
    35.             transform.localRotation = Quaternion.Slerp (transform.localRotation, targetRotation2, smooth * Time.deltaTime);
    36.         }
    37.     }
    38. }
    I'm assuming in the "public void ChangeDoorState" I would have to make it

    Code (CSharp):
    1.     public void ChangeDoorState()
    2. if
    3.         {
    4.         open = !open;
    5.         GetComponent<AudioSource> ().Play();
    6.         }
    7. else
    8.        {
    9.        GetComponet<AudioSource>().Play();
    10.        }
    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!
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    Code (csharp):
    1.  
    2. GetComponent<AudioSource> ().PlayOneShot( DoorCloseSound );
    3.  
    There are differences in behaviour between Play() and PlayOneShot() you probably won't need to know and won't notice those differences here though.
     
  3. wrm186

    wrm186

    Joined:
    Apr 4, 2010
    Posts:
    661
    I've tried that as well before actually posting, but it still only played the open sound. However, I only had this:

    Code (CSharp):
    1.     public void ChangeDoorState()
    2.         {
    3.         open = !open;
    4.         GetComponent<AudioSource> ().PlayOneShot();
    5.         }
    and tried these as well with no avail...
    Code (CSharp):
    1.   public void ChangeDoorState()
    2.         {
    3.         open = !open;
    4.         GetComponent<AudioSource> ().PlayOneShot(DoorOpenSound);
    5.         Get Componet<AudioSource>().PlayOneShot(DoorCloseSound);
    6.        }
    Code (CSharp):
    1.   public void ChangeDoorState()
    2. if  
    3. {
    4.         open = !open;
    5.         GetComponent<AudioSource> ().PlayOneShot(DoorOpenSound);
    6. }
    7. else
    8. {
    9.         Get Componet<AudioSource>().PlayOneShot(DoorCloseSound);
    10. }
    Code (CSharp):
    1. public void ChangeDoorState()
    2.         {
    3.         open = !open;
    4.         GetComponent<AudioSource> ().PlayOneShot(DoorOpenSound);
    5.         open = false
    6.         Get Componet<AudioSource>().PlayOneShot(DoorCloseSound);
    7.        }
    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.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, well if you have 2 audio sources, the simplest way is just create 2 variables:
    Code (csharp):
    1.  
    2. // drag and drop these 2 in the inspector!
    3. public AudioSource openAudioSource;
    4. public AudioSource closeAudioSource;
    5.  
    6. // later, when opening/closing..:
    7. open = !open;
    8. if(open) openAudioSource.Play();
    9. else closeAudioSource.Play();
    10.  
    Side note, not really relevent if you get this working.. but your "if" statement in some examples you wrote were invalid.
     
  5. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    You don't need two audiosources, you'll only ever be playing one of those sounds at a given time.

    Code (csharp):
    1.  
    2.  
    3. public AudioClip DoorOpenSound;
    4.  
    5. public AudioClip DoorCloseSound;
    6.  
    7. public AudioSource AudSource;
    8.  
    9. ...
    10.  
    11. void Start() {
    12.  
    13.  //Cache the audiosource, don't use GetComponent more than once if you can help it
    14.  
    15.  AudSource = GetComponent < AudioSource > ();
    16.  
    17. }
    18.  
    19. ...
    20.  
    21. public void ChangeDoorState() {
    22.  
    23.  if (AudSource.isPlaying) {
    24.  
    25.   //Stop the audiosource if it's already playing
    26.  
    27.   AudSource.Stop();
    28.  
    29.  }
    30.  
    31.  if (open) {
    32.  
    33.   //Assign DoorOpenSound clip
    34.  
    35.   AudSource.clip = DoorOpenSound;
    36.  
    37.  }
    38.  
    39.  else {
    40.  
    41.   //Assign DoorCloseSound clip
    42.  
    43.   AudSource.clip = DoorCloseSound;
    44.  
    45.  }
    46.  
    47.  open = !open;
    48.  
    49.  AudSource.Play();
    50. }
    51.  
     
    theANMATOR2b likes this.
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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.
     
  7. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    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.

    Code (csharp):
    1.  
    2. public void ChangeDoorState()
    3. {      
    4.   open = !open;
    5.   if( open ) // we have just changed the state to open == true so play open sound
    6.   {
    7.     GetComponent<AudioSource> ().PlayOneShot(DoorOpenSound);
    8.   }
    9.   else // we have just changed the state to open == false so play close sound
    10.   {      
    11.     GetComponent<AudioSource>().PlayOneShot(DoorCloseSound);      
    12.   }
    13. }
    14.  
     
    ronie635 likes this.
  8. wrm186

    wrm186

    Joined:
    Apr 4, 2010
    Posts:
    661
    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!
     
  9. wrm186

    wrm186

    Joined:
    Apr 4, 2010
    Posts:
    661
    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!
     
  10. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    The key to learning is to break it down into it's smallest parts.

    Learn if statements inside and out.
    Then learn function calls
    Then learn logic
    Then learn... etc etc..

    If you have complete understanding of each thing at play you'll be able to combine those things without issue later on.
     
    theANMATOR2b likes this.