[solved] Bouncing ball sticks to other coliders

I have been bangin’ my head on this now for hours…very frustrating and it’s probably some simple thing I am overlooking. First off here is my code for the ball. It works just fine.

using UnityEngine;
using System.Collections;

public class Ball_Main : MonoBehaviour {

private Paddle paddle;
private bool hasStarted = false;
private Vector3 paddleToBallVector;

public float velocityXvector;
public float velocityYvector;

// 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;

		if (Input.GetMouseButtonDown (0)) {
			hasStarted = true;
			this.rigidbody2D.velocity = new Vector2 (velocityXvector, velocityYvector);
		}
	}
}

void OnCollisionEnter2D (Collision2D collision){
	if (hasStarted == true){
		audio.Play();
	}

}

}

Just some further details before I describe my problem:

I have a physics material that is friction zero bounce one. The ball from the above script works perfectly and bounces etc. just as in the course.

I finished this section of the course and decided to try my hand at adding a power-up to the game. I decided the simplest thing to start with would be an extra balls power-up. I cloned one of the bricks and removed the sprite. So I have just the collider. I then added a script for this object. I called the thing “ExtraBalls” It is a prefab. Here is a screen shot of the inspector. You will see that there are a lot of public variables exposed.

I then created several alternate ball sprites of different colors.

I also wrote a separate script for each of these balls. I could have left the “…velocity X” and “…velocity Y” public variable on the ball prefabs but I wanted to be able to change everything around when I add the Power Up rather than jumping around to all the prefabs to change the values. Basically, this means that I had get the two scripts communicate.

So anyway, here is the script for the “ExtraBalls” PowerUP

using UnityEngine;
using System.Collections;

public class ExtraBallsPowerUp : MonoBehaviour {

public int hitsToBreak;
public static int breakableCount_PU = 0;
public GameObject extraBallParticle;
public GameObject extraBall_01;
public float yellowBallVelocityX;
public float yellowBallVelocityY;
public GameObject extraBall_02;
public float blueBallVelocityX;
public float blueBallVelocityY;
public GameObject extraBall_03;
public float greenBallVelocityX;
public float greenBallVelocityY;
public GameObject extraBall_04;
public float magentaBallVelocityX;
public float magentaBallVelocityY;
public GameObject extraBall_05;
public float purpleBallVelocityX;
public float purpleBallVelocityY;
public bool yellowBall;
public bool blueBall;
public bool greenBall;
public bool purpleBall;
public bool magentaBall;
public bool powerUpDestroyed = false;

private int timesHit_EBPU;
private bool isBreakable;


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

void OnCollisionEnter2D(Collision2D col){
	if (isBreakable) {
		HandleHits ();
	}
}

void HandleHits(){
	timesHit_EBPU ++;
	int maxHits = hitsToBreak;
	if (timesHit_EBPU >= maxHits) {
		breakableCount_PU++;
		ExtraBalls ();
		Destroy (gameObject);
		powerUpDestroyed = true;
	} 
}

void ExtraBalls(){
	if (breakableCount_PU > 1) {
		if (yellowBall == true) {
			Instantiate (extraBallParticle, transform.position, Quaternion.identity);
			Instantiate (extraBall_01, transform.position, Quaternion.identity);
		}
		if (blueBall == true) {
			Instantiate (extraBallParticle, transform.position, Quaternion.identity);
			Instantiate (extraBall_02, transform.position, Quaternion.identity);
		}
		if (greenBall == true){
			Instantiate(extraBallParticle, transform.position, Quaternion.identity);
			Instantiate(extraBall_03, transform.position, Quaternion.identity);
		}
		if (purpleBall == true) {
			Instantiate (extraBallParticle, transform.position, Quaternion.identity);
			Instantiate (extraBall_04, transform.position, Quaternion.identity);
		}
		if (magentaBall == true) {
			Instantiate (extraBallParticle, transform.position, Quaternion.identity);
			Instantiate (extraBall_04, transform.position, Quaternion.identity);
		}
	}
}

}

It works fine…I’m getting tot he problem soon!

Here is the script for the Yellow Ball prefab (all of the powerUp extra balls have this script). This is different from the normal ball because there is no mouseclick to trigger its motion. Here is the script.

using UnityEngine;
using System.Collections;

public class ExtraBall_Yellow : MonoBehaviour {

private Paddle paddle;
private ExtraBallsPowerUp powerUp;

private float yellowballX;
private float yellowballY;
private bool destructionOccured;


// Use this for initialization
void Start () {
	powerUp = GameObject.FindObjectOfType<ExtraBallsPowerUp>();
	yellowballX = GameObject.Find ("ExtraBalls").GetComponent<ExtraBallsPowerUp>().yellowBallVelocityX;
	yellowballY = GameObject.Find ("ExtraBalls").GetComponent<ExtraBallsPowerUp>().yellowBallVelocityY;
	destructionOccured = GameObject.Find ("ExtraBalls").GetComponent<ExtraBallsPowerUp>().powerUpDestroyed;
	Debug.Log (destructionOccured);
}

 	void Update () {
	if (destructionOccured == true) {
		this.rigidbody2D.velocity = new Vector2 (yellowballX, yellowballY);
	}
}

void OnCollisionEnter2D (Collision2D collision){
	audio.Play();
}

}

Now finallly here is the problem. The power up breaks when the main ball impacts it. Particles fly as planned.
Then the extra balls come out as planned. They do so with the velocity as planned. The screen shot below shows the problem:

The yellow ball just broke the blue brick…then it impacts the hexagons along the border and sticks there. The hexagons are individual sprites with polygon colliders. (They randomly rotate a few degrees at a time and placed in the scen at predetermined locations by another script.)

The red ball just bounces off these hex border pieces like you would expect. The yellow ball is not simply stuck in place. It is actually bouncing a tiny bit up and down. I looked at in 3D and stepped through frame by frame and can see it hit the hex and bounce down then it is as if it hits some invisible object and bounces back. I have no other object in the scene other than those which are visible in the screen shot. This must be a code issue but i just can’t figure it out.

Thanks for the detailed description. It’s kinda hard to determine the problem without playing with it. Are you able to upload your project to Git so we can see it as per this?

It took me a couple of days to get back to this…Work, work, work!..sigh.

Anyway, I finally set up a git repo for this project this morning. Here is the link:

Solution: In my ball script I had the following:

void Update () {
	if (destructionOccured == true) {
		this.rigidbody2D.velocity = new Vector2 (yellowballX, yellowballY);
	}

This put me in a “loop”, so to speak, where since destructionOccured was always true it was literally instantiating the ball thousands of times.

This fixed the problem:

void Update () {
	if (destructionOccured == true) {
		this.rigidbody2D.velocity = new Vector2 (yellowballX, yellowballY);
		destructionOccured = false;
	}
2 Likes