- Home /
Brick Breaker - Ball not bouncing back from the brick.
I have a problem. When my ball is touching a brick it does not bounce back from that brick, but it gets through that brick and the brick is destroyed. Any idea why?
using UnityEngine;
using System.Collections;
public class Brick : MonoBehaviour {
public int maxHits;
private int timesHit;
private LevelManager levelManager;
// Use this for initialization
void Start () {
timesHit = 0;
levelManager = GameObject.FindObjectOfType<LevelManager>();
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D(Collision2D col){
timesHit++;
if (maxHits == timesHit) {
Destroy (gameObject);
}
}
//TODO change this later;
void SimulateWin() {
levelManager.LoadNextLevel ();
}
}
This is my Brick script. What I want to achieve is to destroy that brick and my ball bounce from that before it gets destroyed.
Answer by NinjaISV · Nov 25, 2015 at 09:03 PM
There is no code making it bounce back, and because it's destroying the brick, there's nothing there to bounce back from. You should just inverse the y velocity of the ball's Rigidbody2D before you destroy the gameobject in the OnCollisionEnter2D
function.
EDIT: (Thanks to @fafasefor helping improve this answer!) To do this, just add this line of code to the top of your script public Rigidbody2D ball;
then go to the inspector, assign the ball to the to the class. Now just add this line of code ball.velocity = new Vector2 (ball.velocity.x, -ball.velocity.y)
to the`OnCollisionEnter2D` function. All this is doing is inverting the balls y velocity to send it back down.
Your class should now look like
using UnityEngine;
using System.Collections;
public class Brick : MonoBehaviour {
public int maxHits;
private int timesHit;
public Rigidbody2D ball;
private LevelManager levelManager;
// Use this for initialization
void Start () {
timesHit = 0;
levelManager = GameObject.FindObjectOfType<LevelManager>();
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D(Collision2D col){
timesHit++;
if (maxHits == timesHit) {
ball.velocity = new Vector2 (ball.velocity.x, -ball.velocity.y)
Destroy (gameObject);
}
}
//TODO change this later;
void SimulateWin() {
levelManager.LoadNextLevel ();
}
}
Note that I changed the ball's velocity before destroying the object, this is very important. If I had destroyed it first, then the velocity would never have been changed.
Ok. Can you give me an example? I'm a total beginner and I try to learn this thing and that's how I got here, watching a tutorial from an older version of unity (4.6).
Is your collider2d component set to "trigger"? if it is, it will pass through.
He said the brick is destroyed, so it must be calling the function.
Looks like a wrong example here. The bouncing should be reflecting the current direction onto the plane using the normal of it. Pretty much using Vector3.Reflect(rigidbody.velocity, hit.normal)
Right, that's much smarter! I'll add that. Thanks!
What about using OnCollisionExit2D? I'd imagine the brick would get destroyed after the physics engine has handled the bounce. This way side hits would be handled correctly as well ( invert velocity.x )
I had to change the var from public
to private
because I work with prefabs and I don't want to set the script every single time, I want to use the same script for all bricks prefabs and to add ball = GameObject.FindObjectOfType();
in order to pick my ball. Now works perfectly fine. Thanks guys. :D
Answer by Negatiw · Jul 03, 2016 at 06:26 PM
Click on your Ball object, and go to its RigidBody2D, there is a setting that is named Collision Detection, it's very likely that yours is on Discreet, change it and put it on Continuous and it should work like a breeze!
Your answer
Follow this Question
Related Questions
Trigger2D not working 0 Answers
2D Top Down Pathfinding 0 Answers
Player vibrating when falling 1 Answer
How to add collision to 2d animation 1 Answer
The player randomly freezes in place while other objects move ingame 1 Answer