Bounce plays always

I dont know what happened, but the bounce sound plays whenever the ball hits the bricks. The crack sound also plays. Is there a way to fix this?

Hi @Nathan_Hunter,

There sure is, at the moment you most likely have the Boing audio clip being played via the OnCollisionEnter2D method in the Ball script, and the Crack audio clip being played via the OnCollisionEnter2D method in the Brick.

Both of those locations make sense for each of the individual objects, but the Ball perhaps should be considering what it is making a collision with before playing the audio clip. You could achieve that with something like this;

private void OnCollisionEnter2D(Collision2D collision)
{
    if (hasStarted)
    {
        if(!collision.gameObject.GetComponent<Brick>())
        {
            this.GetComponent<AudioSource>().Play();
        }
    }
}

In the above, the added if statement says "if the thing I’ve collided with is not a brick, play the audio clip.

You can simplify the above by merging the two if statements using an && operator, but for the explanation and readability I’ve left it in long-hand if you will :slight_smile:

Just something to be aware of, if you were to add an indestructible brick, you won’t get a crack or a boing now, as a player I would expect a boing in this scenario…

private void OnCollisionEnter2D(Collision2D collision)
{
    if (hasStarted)
    {
        if(!collision.gameObject.GetComponent<Brick>() || !collision.gameObject.GetComponent<Brick>().isBreakable)
        {
            this.GetComponent<AudioSource>().Play();
        }
    }
}

In the above, we have added a second condition, so it effectively says “if the thing I’ve collided with is not a brick, or, it’s not a breakable brick, play the audio clip”.

You would also need to change the isBreakable variable in the Brick class to be public not private for this to work.

Hope this helps. :slight_smile:

2 Likes

I added in the code, and made the collisions into Collision2D, but now there is an error: “An object reference is required to access non-static member ‘UnityEngine.Collision2D.gameObject’”. What is going on?

Could you pop a screenshot up of the error and also the full script for me please.

Here you go:

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {

private Paddle paddle;
private bool hasGameStarted = false;
private Vector3 ballToPaddleVector;

// Use this for initialization
void Start () {
	paddle = FindObjectOfType<Paddle>();
	ballToPaddleVector = this.transform.position - paddle.transform.position;
}

// Update is called once per frame
void Update () {
	if (!hasGameStarted) {
		this.transform.position = paddle.transform.position + ballToPaddleVector;
		
		if (Input.GetMouseButtonDown(0)) {
			this.rigidbody2D.velocity = new Vector2(2f, 8f);
			hasGameStarted = true;
		}
	}
}

void OnCollisionEnter2D(Collision2D Collision) {
	Vector2 tweak = new Vector2 (Random.Range(-0.2f, 0.2f), Random.Range(-0.2f, 0.2f));
	
	if (hasGameStarted) {
		if(!Collision2D.gameObject.GetComponent<Brick>() || !Collision2D.gameObject.GetComponent<Brick>().isBreakable) {
			audio.Play();
		}
		this.rigidbody2D.velocity += tweak;
	}
}

}

I got rid of the error by replacing lines 31 to 34 with

if (hasGameStarted) {
if(!this.rigidbody2D.GetComponent() || !this.rigidbody2D.GetComponent ().isBreakable) {
audio.Play();
}

, but the bounce still plays when hitting a brick.

Hi Nathan,

You didn’t copy the code I gave you quite as it was provided.

In the if statement in my example you will note I am referencing collision which is the name of the parameter being passed into the OnCollisionEnter2D method, but in your example, you have the type instead, e.g. Collision2D

Try changing the code back to what I provided and let me know what happens.

1 Like

OK, this worked, but my parameter was Collision, not collision. I only just now realized this. Thanks!

1 Like

Glad it is all working for you now Nathan :slight_smile:

1 Like