- Home /
Sounds triggering too fast
So, I've posted about this before, but I've tried every suggestion I've been given with the same issue always arising so I feel like something is just missing from the code at this point. I have a walking sound array being triggered by the arrow keys when I press them down, the sound clips in the array will change depending on which kind of surface (ex. wood, grass, dirt, etc.) my character is on and I have the sounds being changed via OnCollisionColliderHit using tags as a reference point for the change. Everything is being triggered on the right surfaces but the only issue is that the sound is being called every frame-ish. I don't have anything in the Update function so not sure what's going on. I'm still quite new to scripting so apologies if I'm making a rookie mistake.
using UnityEngine;
using System.Collections;
public class WormWalk : MonoBehaviour
{
public AudioSource Audio;
public AudioClip[] walkingSounds;
public int pitchRandomness = 1;
// Use this for initialization
void Start ()
{
Audio = (AudioSource)gameObject.GetComponent ("AudioSource");
}
// Update is called once per frame
void Update ()
{
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
if(Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
{
Audio.Play();
}
else
{
Audio.Stop ();
}
if (hit.gameObject.tag == "groundFloor")
{
print("Ground");
Audio.clip = walkingSounds[0];
}
if (hit.gameObject.tag == "woodFloor")
{
print("Wood");
Audio.clip = walkingSounds[1];
}
if (hit.gameObject.tag == "stoneFloor")
{
print("Stone");
Audio.clip = walkingSounds[2];
}
if (hit.gameObject.tag == "templeFloor")
{
print("Temple");
Audio.clip = walkingSounds[3];
}
}
}
Everything is being triggered on the right surfaces but the only issue is that the sound is being called every frame-ish.
So the issue is that sound is playing continously?
In a sense yes. I have a worm character and I'm just wanting a few loops I made of, for example, the character sliding on top of grass to just play the loop and keep looping as I have the keys down. The issue is that the loop isn't playing to completion, it's a 10 second loop but it's only playing the first 0.1 seconds and rapidly (one after another). So I'm assu$$anonymous$$g my solution is to just get the loop to play all the way through and loop properly. I'm just not sure how to go about that, I've dug through many different methods but not having any luck.
Have you checked if Audio.Play();
is being called constantly?
Answer by Moor · Nov 21, 2013 at 05:38 AM
try this
if(Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
{
Audio.Play();
}
else if(Input.GetButtonUp("Horizontal") || Input.GetButtonUp("Vertical"))
{
Audio.Stop ();
}
Just plugged that code in but still having the sound issue :/ but thank you very much for the suggestion!
take all the print statement out and use
void OnControllerColliderHit(ControllerColliderHit hit)
{
if(Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
{
else if (hit.gameObject.tag == "groundFloor")
{
audio.PlayOneShot( walkingSounds[0], 1.0);
}
else if (hit.gameObject.tag == "woodFloor")
{
audio.PlayOneShot( walkingSounds[1], 1.0);
}
else if (hit.gameObject.tag == "stoneFloor")
{
audio.PlayOneShot( walkingSounds[2], 1.0);
}
else if (hit.gameObject.tag == "templeFloor")
{
audio.PlayOneShot( walkingSounds[3], 1.0);
}
}
}
else if(Input.GetButtonUp("Horizontal") || Input.GetButtonUp("Vertical"))
{
Audio.Stop ();
}
Thanks again for the suggestion! On an upside the overlapping sound issue sounds different now but I'm still having an issue overall with not being able to just loop a single sound (per surface)
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow) && !audio.isPlaying){
audio.clip = walkingSounds[i];
audio.Play();
}
Answer by static_cast · Nov 11, 2013 at 04:18 PM
Audio.pitch = (1);
Apparently audio.pitch changes the speed of your sound, so try altering it to below your pitch to see if it plays correctly.
Gave it a shot but no luck, thanks for the suggestion though!
Answer by loboclerk · Nov 12, 2013 at 07:21 AM
It looks like OnControllerColliderHit is being called repeatedly.
Maybe just run Audio.Play() each time the clip is changed.
public AudioClip prevAudio = null;
// for each instance where you assign Audio.clip
prevAudio = Audio.clip;
Audio.clip = xxxxxxxxx;
if (prevAudio!=Audio.clip) {
Audio.Play();
}
else {
prevAudio = null;
Audio.Stop();
}
Is there another way I could get the sounds to switch via a tag-reference method or some other means? And I don't think I fully understand the code you posted (I'm still new to coding, I tried to connect the dots in my head as to how to plug this code into what I currently have going on and can't think of a good way to do so).
Answer by Remingsworth · Apr 01, 2015 at 12:28 PM
I too agree, as loboclerk suggested, that the audio is being called repeatedly. If this is the case then perhaps try using a bool to prevent the audio from playing if it is already in the process of playing. A .cs and .js of this can be found in a similar post here: http://answers.unity3d.com/questions/301797/audioplayoneshot-update-mess.html
Your answer
Follow this Question
Related Questions
Need help with scripting bug fix. pleease 0 Answers
Yet another Audio on Collision issue 1 Answer
Collision sounds doubling up. 1 Answer
Problem Detecting 2D Collisions 1 Answer
Multiple Cars not working 1 Answer