How to make door sound effects play correctly when entering from either side of the door

[UPDATE: IT’S NOW SEMI-RESOLVED, JUST HAVE A NEW BUG TO DEAL WITH. READ TOO SEE WHERE I AM LATELY]

Hi everyone

I’m learning Unity and am not experienced.

I successfully followed this tutorial and made the door make an opening/closing sound when it opens/closes. The door opening and its sound effect are achieved by walking into a trigger placed in front of the door.

However, I need to make the door open from the other side as well and then not trigger the sound effect when walking into the trigger on the other side of the door. And of course when the door closes, I need the sound effect to just play once instead of twice.

If anyone knows how to do this or of videos that show you how to do this, I would appreciate it.

Thanks

Can you not just add a trigger to the other side with its own code? It sounds like you have all the tools you need to do it.

I don’t know how to turn off both triggers while the door is open, or if I should do it in the same script. I don’t know how to code it, it’s not yet apparent to me in the code I already have. I have so far just duplicated the trigger and put it on the other side of the door and that has resulted in the problem I’ve mentioned.

Isolate and separate the data flow:

  • player enters the trigger: desired door state should be OPEN

  • player exits the trigger: desired door state should be CLOSED

In its Update(), have the door observe the desired state and compare it to its current state.

If those states are ever out of sync, resolve appropriately and update current state.

I’m afraid that doesn’t help me at all. I am still learning Unity and C#, I am not experienced. I need it explained simply how to make the door opening and closing sound effect play just once when entering from both sides of the door.

Here is the code I have so far.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DoorOpenFirst : MonoBehaviour

{
public GameObject theDoor;
public AudioSource doorFX;

void OnTriggerEnter(Collider other)

{
doorFX.Play();
theDoor.GetComponent().Play(“DoorOpen”);
this.GetComponent().enabled = false;
StartCoroutine(CloseDoor());
}

IEnumerator CloseDoor()

{
yield return new WaitForSeconds(5);
doorFX.Play();
theDoor.GetComponent().Play(“DoorClose”);
this.GetComponent().enabled = true;
}

}

Oh sounds like you’re missing Step 2. Read below for Step 2:

Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

How to do tutorials properly, two (2) simple steps to success:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes.
BE PERFECT IN EVERYTHING YOU DO HERE!!

If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.
Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

The purpose of this forum is to assist people who are ready to learn by doing, and who are unafraid to get their hands dirty learning how to code, particularly in the context of Unity3D.

This assumes you have at least written and studied some code and have run into some kind of issue.

If you haven’t even started yet, go check out some Youtube videos for whatever game design you have in mind. There are already many examples of the individual parts and concepts involved, as there is nothing truly new under the sun.

If you just want someone to do it for you, you need go to one of these places:

https://forum.unity.com/forums/commercial-job-offering.49/

https://forum.unity.com/forums/non-commercial-collaboration.17/

https://livehelp.unity.com/?keywords=&page=1&searchTypes=lessons

I think maybe you didn’t understand that I was not looking for help with something that the tutorial tells you how to do. I was looking for help with something the tutorial does not tell you how to do.

But just last night, after thinking hard, I came up with an idea: Expand the size of the box acting as the door trigger so that it reaches the other side of the door too. The same trigger for both sides of the door, and so there is only one door opening and closing sound, the way it should be. It works and so far I don’t see a problem with it other than a new bug it presents: If you open the door and stand in the doorway for long enough, the DoorOpen animation just instantly starts again, the door shuts instantly without moving shut - not the way it was animated to close, and then it moves open again the way it was animated to open. So I wonder how to resolve this.

Thank god I came up with this idea at least because it didn’t feel like anyone was going to tell me a solution at least soon :stuck_out_tongue:

So no coding involved in this solution. I posted this question in this category because I just speculated the solution could be related to coding.

Hi again

After thinking hard last night, I came up with an idea: Expand the size of the box acting as the door trigger so that it reaches the other side of the door too. The same trigger for both sides of the door, and so there is only one door opening and closing sound, the way it should be. It works and so far I don’t see a problem with it other than a new bug it presents: If you open the door and stand in the doorway for long enough, the DoorOpen animation just instantly starts again, the door shuts instantly without moving shut - not the way it was animated to close, and then it moves open again the way it was animated to open. So I wonder how to resolve this.

So no coding involved in this solution. I posted this question in this category because I just speculated the solution could be related to coding.

So, the code you used to open the door can almost be exactly the same as the code to close the door. Just put the closing of the door in OnTriggerExit, mirroring what they did in OnTriggerEnter in the video.

Thank you, I’ll look into that, see what I can do

You do not find tutorials that tell you exactly what to do.

That’s not a thing. I’m sorry you may have been mislead into thinking it was a thing. It’s just not a thing.

Instead, you do tutorials to understand how something works.

Then when you understand how a system works, implementing this:

… becomes trivial.

Welcome to software engineering, and actually all engineering in general.