Ball/Brick Collisions Counts as Two Hits

If anyone knows how to fix this problem with anything other than just multiplying the maxHits by 2 please let me know. I dont know whats causing this so I dont know which code to post but if anyone has suspicions let me know and ill post the corresponding code or unity settings I have.

Hi Oscar,

Please can you post your full scripts for both Ball.cs and Brick.cs.


See also;


using UnityEngine;
using System.Collections;

public class Brick : MonoBehaviour 
{
	private LevelManager levelManager;
	private int timesHit;
	private bool isBreakable;
	 
	public AudioClip crack;
	public Sprite[] hitSprites;
	public static int breakableCount = 0;

	// Use this for initialization
	void Start () 
	{
		isBreakable = (this.tag == "Breakable");
		// Keep Track of Breakable Bricks
		if (isBreakable)
		{
			breakableCount++;
			print (breakableCount);
		}
		timesHit = 0;
		levelManager = GameObject.FindObjectOfType<LevelManager>();
	}
	
	void OnCollisionEnter2D (Collision2D col)
	{
		AudioSource.PlayClipAtPoint (crack, transform.position);
		if (isBreakable)
		{
			HandleHits();
		}
	}

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

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

	void SimulateWin ()
	{
		levelManager.LoadNextLevel();
	}
}
using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour 
{
	private Paddle paddle;
	private bool hasStarted = false; 
	private Vector3 paddleToBallVector;
	// Use this for initialization
	void Start () 
	{
		paddle = GameObject.FindObjectOfType<Paddle> ();
		paddleToBallVector = this.transform.position - paddle.transform.position;
	}
	
	// Update is called once per frame
	void Update () 
	{
		if (!hasStarted)
		{
		this.transform.position = paddle.transform.position + paddleToBallVector; //Lock Ball to Paddle

			// Waits for mouse click to launch
			if (Input.GetMouseButtonDown(0))
			{
			print ("Mouse Clicked, Launch Ball");
			hasStarted = true;
			this.rigidbody2D.velocity = new Vector2 (2f, 10f);
			}
		}
	}

	void OnCollisionEnter2D (Collision2D collision)
	{
		if (hasStarted)
		{
		audio.Play ();
		}
	}
}
1 Like

Sorry for the delay, I took a break over Christmas.

1 Like

Thanks for posting the scripts Oscar.

Next question - how do you know you are getting the collision detected twice? What have you already tried/done to test/prove this?

I have manipulated the max hits variable to produce the following results:
Maxhit= 1, 1 hit to break
Maxhit= 2, 1 hit to break
Maxhit= 3, 2 hits to break
Machit= 4, 2 hits to break
Maxhit= 5, 3 hits to break


Thats as far as I went but I concluded that it was counting every hit as 2.

The only reason I could think of is maybe you have two colliders on your bricks? If you have two colliders on your brick at the same time, the ball could bounce off of them both at the same time causing it to count twice.

If that isn’t the cause of the issue, in void HandleHits() you could multiply maxHits by 2 to make it compensate for the bug.

In void HandleHits(), instead of int maxHits = hitSprites.Length + 1; I would do
int maxHits = hitSprites.Length * 2; maxHits += 1;

Be aware that this can lead to problems in void LoadSprites as well since timesHit is counted twice every time. So you could change int spritesIndex = timesHit - 1; into
int spritesIndex = (timesHit / 2) - 1;

Hi Oscar,

Check the bricks in the scene, select one, then look in the Inspector. Confirm you only have one Brick script attached.

Check the prefabs also.

You guys were right about the double colliders. Dont know how I missed that one, Thanks guys

1 Like

Glad to hear you have this resolved now Oscar and can continue forward.

Kudos to Jared (@Jarhead_Junior) for the good call :slight_smile:

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

Privacy & Terms