Sound Effects Either Don't Play or Are Muffled Extremely

Hello, I’m having a strange error with sound effect playback in the block breaker game. I’ve done all the steps for boing and it works properly. However, it does not seem to be working with the crack sound. Well, I think it’s not?

What I mean is that the sound I imported I know is audible and loud, and when I apply the crack, there’s like a 50/50 chance it plays on hitting a brick and on the ones it does play it’s been muffled to barely being audible. I tried with both mono and stereo audio tracks and I have the same issue both times. Even more strange is the fact that everytime the ball hits the brick, a “one shot audio” appears in the hierarchy. So it’s being called I think, just not heard? I’m using Unity 5.3, sorry I know it says to use the other one but I already started with this one.

Here’s my Brick code for reference:

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

public class Brick : MonoBehaviour { 

	public AudioClip crack;
	public Sprite[] hitSprites;
	public static int breakableCount;

	private int timesHit;
	private LevelManager levelManager;
	private bool isBreakable;


	// Use this for initialization
	void Start () {
		isBreakable = (this.tag == "Breakable");
		if (isBreakable) {
			breakableCount = breakableCount + 1;
		}

		timesHit = 0;
		levelManager = GameObject.FindObjectOfType<LevelManager> ();
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	void OnCollisionExit2D(Collision2D collider){
		AudioSource.PlayClipAtPoint (crack, transform.position);
		if (isBreakable) {
			HandleHits ();
		}
	}

	void HandleHits(){
		timesHit = timesHit + 1;
		int maxHits = hitSprites.Length + 1;
		if (timesHit >= maxHits) {
			breakableCount--;
			levelManager.BrickDestroyed ();
			Destroy (gameObject);
		} else {
			LoadSprites ();
		}
	}

	void LoadSprites(){
		int spriteIndex = timesHit - 1;
		if(hitSprites[spriteIndex]){
		this.GetComponent<SpriteRenderer> ().sprite = hitSprites[spriteIndex];
		}
	}

	//TODO Remove this method once we can actually win.
	void SimulateWin(){
		levelManager.LoadNextLevel ();
	}
}

Hi Bucadyz,

I would try changing your OnCollisionExit2D to OnCollisionEnter2D.

Then I would amend the code within that method slightly to allow for scenarios where the brick being hit isn’t breakable, you don’t actually want to play the crack sound at that point;

void OnCollisionExit2D(Collision2D collider)
{
	if (isBreakable) 
	{
		AudioSource.PlayClipAtPoint (crack, transform.position);
		HandleHits ();
	}
}

With regards to the audio clip being muffled, this one is perhaps a little harder to resolve remotely, but you could try specifically setting the volume within the PlayClipAtPoint method, like so;

		AudioSource.PlayClipAtPoint (crack, transform.position, 1f);  // adjust as necessary but start with 1f

Hope this helps :slight_smile:


See also;

Hello,

Thank you very much for the ideas! Alas, it still seems to be acting weird. Something I just noticed now is that it seems like the sound is actually coming from a different location every time (when it actually plays). So sometimes it’ll come out the left earphone and others the right. I’m pretty sure it’s not on 3D sound so I’m not sure what this could be. No worries if you’re helping others though. Thanks for the reply!

I think I figured it out! At least I’m close to figuring it out. So, there’s no 3D Sound setting in Unity 5 but there is a spatial sound slider. I messed with the spatial sound slider for the boing sound which is working correctly, and it reproduced the same issue I’m having with the crack sound! So the problem now is I don’t know how to access the spatial sound slider for the crack because the crack isn’t being activated with an audio source it’s being activated purely from the script.

Hi,

I’m surprised that Spatial Blend is having any difference on the crack sound effect in your game, I’ve check a course copy of this project which has is using the PlayClipAtPoint method for the crack audio clip and whilst this does default to 3D (spatial blend 1.0f), it’s still very clear and plays.

Could you zip up your project files and I will happily take a look for you. The forum will allow files of 10mb maximum in a post, so if your project is larger than that, please use a service such as Google Drive or Dropbox and post the URL.

Block Breaker.zip (8.6 MB)

I have uploaded it above. Thank you very much!

Hi,

Thanks for sharing the project.

I didn’t realise you were using Unity 2017, as in your original post you mention Unity 5.3.

I’ve opened the project with the same version and can experience the same issue you are having.

At first I couldn’t really hear the crack sound at all, I checked your Brick prefabs and noticed that you had CrackText.wav set as the AudioClip, this is obviously a very short version and in itself is quite hard to hear anyway, so I changed the prefabs back to Crack.wav to start with for testing.

Next I noticed that when I started your game from the main menu, the music was quite loud, so I disabled it.

The boing sound you have is lovely, but to try and hear the crack sound was a distraction, so I edited your code so that it wouldn’t play the boing sound.

I ran the game again and I could hear a faint crack sound play. I managed to pause the game at the point of collision with a brick and looked at the One Shot Audio in the Hierarchy and could see that the spatial blend was supporting 3D.

As discussed, there isn’t a way to change that using PlayClipAtPoint without creating your own PlayClipAtPoint script instead, this would give you access to all of the properties of the AudioSource and thus you could change the spatial blend.

I check some other things also, the settings on the .wav files, the sound settings for the project.

I also tried increasing the volume of the clip in code, e.g. changing 1f to 2f and then to 10f, nothing made any difference.

It occurred to me that with the 3D setting, Unity is applying a roll off on the volume to allow for the distance to the AudioListener from the AudioSource. In our case, the AudioListener is attached to the MainCamera.

So, rather than re-write the PlayClipAtPoint method, I simply set the AudioClip to play at the MainCamera’s position - problem solved :slight_smile:

In order to do this, add the following to your Brick.cs script;

public class Brick : MonoBehaviour 
{
    private static Camera mainCamera;

   // ...

Now, within the Start method, add the following;

if (!mainCamera)
{
    mainCamera = GameObject.FindObjectOfType<Camera>();
}

The first item is a private member variable which we will use to hold a reference to the main camera.

The second piece of code checks to if the variable is null, if it is, then it finds the first GameObject of type Camera and sets our variable.

I have set the member variable to be static so that it the variable belongs to the class, not an instance of the class, and then check to see if it is null so that the FindObjectOfType method is only ever called once per scene, not once for every brick in the scene. :slight_smile:

Finally, in the OnCollisionEnter2D method, we need to change the position at which the AudioClip is played, we can do that using the following;

void OnCollisionEnter2D(Collision2D collider)
{
    AudioSource.PlayClipAtPoint (crack, mainCamera.transform.position, 1f);
    // ....
}

This is where we use the camera’s transform position instead of the bricks.

For a more complex game, perhaps with more cameras, it may be worth considering writing your own PlayClipAtPoint method but for Block Breaker I don’t think this is really necessary.

Hope this helps :slight_smile:

Worked like a charm! Thank you very very much, I would have never thought of using the camera’s position. Thank you again!

1 Like

You are very welcome. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms