• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by DarkSealer · Nov 25, 2015 at 08:52 PM · 2d game2d-physics

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.

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment

People who like this

0 Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DarkSealer · Nov 25, 2015 at 09:40 PM 0
Share

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).

avatar image Raimi · Nov 25, 2015 at 11:25 PM 0
Share

Is your collider2d component set to "trigger"? if it is, it will pass through.

avatar image NinjaISV Raimi · Nov 25, 2015 at 11:40 PM 0
Share

He said the brick is destroyed, so it must be calling the function.

avatar image DarkSealer Raimi · Nov 26, 2015 at 07:06 AM 0
Share

It's not. I checked that few times.

avatar image fafase · Nov 26, 2015 at 07:42 AM 0
Share

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)

avatar image NinjaISV fafase · Nov 26, 2015 at 05:36 PM 0
Share

Right, that's much smarter! I'll add that. Thanks!

avatar image NoseKills fafase · Jun 21, 2016 at 04:28 PM 0
Share

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 )

avatar image DarkSealer · Nov 27, 2015 at 07:54 PM 0
Share

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

avatar image

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!

Comment
thanhquing

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thanhquing · Aug 22, 2016 at 01:49 AM 0
Share

Thank you very much. :D

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

*** Trigger Collider2D event only once *** 3 Answers

I am very new to unity and I'm looking for advice on a targeting system( or if i'm setting myself up for failure) 1 Answer

[2D] Moving the player 1 tile at a time using rigidbody movement 0 Answers

Tilemap Collider 2D not generating colliders...,Tilemap Colliders won't generate.... 0 Answers

Need suggestion for tossing game 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges