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);
}
}
}
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 ?
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
Follow this Question
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