Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by OptimisticSoundGuy · Apr 16, 2016 at 05:39 PM · c#audio

Trying to get the doors to play the opening and closing sounds seperately/individually

I set up this script up and the doors enter the open, closed and locked states perfectly. The problem is that when the player opens the door it automatically plays the closing sound when the opening sound has finished and it sounds stupid because the door dosnt close unless the player tells it to. I'll add the script iv got so far below but any help that anyone could lend would be a greatly appreciated, thank you to any and all that respond. @Mavina.

public class new_door : MonoBehaviour {

     public bool open = false;
     public float doorOpenAngle = 90f;
     public float doorCloseAngle = 0f; 
     public float smooth = 40f;
     public bool isLocked = false; 
     public AudioClip openingSound;
     public AudioClip closingSound;
     public AudioClip lockedDoorSound;
     public AudioSource audioSource;

     void Start ()
     {
         audioSource = GetComponent<AudioSource> (); 
         if (audioSource == null)
         { // if AudioSource is missing
             Debug.LogWarning("AudioSource component missing from this gameobject. Adding one.");
             // let's just add the AudioSource component dynamically
             audioSource = gameObject.AddComponent<AudioSource>();
         }
     }

     public void ChangeDoorState()
     {
         if (audioSource != null) // this should not ever be false because we validated it in Start()
         {
         if (isLocked != true) {
             open = !open;
             audioSource.PlayOneShot (openingSound);
             }
             else
             {
                 PlaylockedDoorSound ();
             }
         }
     }

     void PlaylockedDoorSound()
     {
         if (audioSource != null) // this should not ever be false because we validated it in Start()
         {
             audioSource.PlayOneShot (lockedDoorSound);
         }
     }

     void Update () 
     {
         if(open) // open == true 
         {
             Quaternion targetRotationOpen = Quaternion.Euler(0, doorOpenAngle, 0);
             transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotationOpen, smooth * Time.deltaTime);
         }
         else
         {
             Quaternion targetRotationClose = Quaternion.Euler(0, doorCloseAngle, 0);
             transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotationClose, smooth * Time.deltaTime);
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by OptimisticSoundGuy · Apr 16, 2016 at 09:37 PM

@Mavina heres the interact script that talks to the doors.

public class Interact : MonoBehaviour { public string interactButton;

 public float interactDistance = 3f; 
 public LayerMask interactLayer; 

 public Image interactIcon; 

 public bool isInteracting;

 void Start ()
 {
     if(interactIcon != null)
     {
         interactIcon.enabled = false;
     }
 }

 void Update ()
 {
     Ray ray = new Ray (transform.position, transform.forward);
     RaycastHit hit;

     if (Physics.Raycast (ray, out hit, interactDistance, interactLayer)) {
         if (isInteracting == false) {
             if (interactIcon != null) {
                 interactIcon.enabled = true;
             }

             if (Input.GetButtonDown (interactButton)) {
                 if (hit.collider.CompareTag ("Door")) {
                     hit.collider.GetComponent<new_door> ().ChangeDoorState();
                 } else if (hit.collider.CompareTag ("Key")) {
                     hit.collider.GetComponent<New_key> ().Unlocknew_door ();
                 }
             }
         }     

     } else {
         interactIcon.enabled = false;
     }
 }

}

I dont see how this is the script that will fix the problem though, in the door script i could get it so that both opening and closing sounds would play one after each other. I also had it so that it would play one but never play the other. i thought it would be by messing about with the door script that would fix it ?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TBruce · Apr 16, 2016 at 10:22 PM 0
Share

@OptimisticSoundGuy

Well first I see nowhere in your code that your script plays the "closingSound" clip unless the "lockedDoorSound" and the "closingSound" are the same and what you hear playing is actually the "lockedDoorSound" clip in the PlaylockedDoorSound() function.

Secondly you are calling hit.collider.GetComponent().ChangeDoorState() in the Update() function which occurs every frame. So by the logic in the ChangeDoorState() function, PlaylockedDoorSound() will be called after the openingSound is played.

For a start do this in the Interact class ins$$anonymous$$d of Update

public Camera camera; // you can use your main camera void On$$anonymous$$ouseOver() { if ((camera) && (Input.GetButtonDown(interactButton))) { Vector3 mousePos = camera.ScreenToWorldPoint(Input.mousePosition); if (Physics.Raycast(ray, hit)) { if (hit.collider.CompareTag ("Door")) { hit.collider.GetComponent ().ChangeDoorState(); } else if (hit.collider.CompareTag ("$$anonymous$$ey")) { hit.collider.GetComponent ().Unlocknew_door (); } } } }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

143 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Audio not working on first run of game, but after second run it does 0 Answers

How to use one button to play different audio for different Targets? 0 Answers

Audio clip repeating infinitely in update,Audio clip repeating error 0 Answers

C# Setting AudioMixerGroup through code 2 Answers

How to stop a sound playing once another starts? (C#) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges