• 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 /
  • Help Room /
This question was closed Feb 22, 2016 at 10:17 PM by hexagonius for the following reason:

The question is answered, right answer was accepted

avatar image
Question by DeathMade2015 · Feb 22, 2016 at 09:18 AM · c#unity 5collisionphysics2dupgrade

[SOLVED] Problem with OnCollisionEnter2D script after upgrading to Unity 5.3(from Unity 4.6)

Hello guys! I'm currently doing a Unity course on Udemy. I'm a newbie so you might see some really bad coding skills and if you can help me in a friendly manner it would be appreciated. Also this is my first question here so if I have made it wrong, feedback is also appreciated :)

I have simple block breaker type of game. But after upgrading to Unity 5.3 my game broke.

I have a ball and bricks in a scene. Ball hits a brick and the brick gets destroyed. I wanted to make it a bit more interesting so I added a piece of code that after collision the ball bounces off the brick and it makes the brick lose its box collider, brick is set to not kinematic, and the brick falls down through everything off the screen.

This is not happening however. Instead the ball flies THROUGH the brick(and other bricks because there are more of them in the scene) instead of bouncing off of it, and the brick falls down.

Expected Behavior: Ball collides with a brick, Ball bounces off the brick and that brick falls down off the screen and destroys itself

What is happening: Ball flies through the brick, doesnt bounce and the brick falls down off the screen.

I tracked it down and it is the line where I turn off the box collider. If I comment it it will bounce and everything "works". If i uncomment it it flies through. To me it seems like the physics(bounce) is applied after the brick has the box collider turned off. It works exactly like if it was set to trigger. Thing is this worked perfectly in unity 4.6 and doesn't in Unity 5.3. And no, I don't have either of them(brick or ball) set to trigger

Also I've looked into unity 5 changelog and they say they made changes to OnCollisionEnter2D but I don't understand what they mean. If someone can explain to me what changes have been made and why my code is not working in Unity 5.3 it would be EPIC!

Thanks in advance and here is the code:

 void something() {
     
     brickCount--;
     GameObject.FindObjectOfType<LevelManager>().blocksDestroyed();
     rb.isKinematic = false;
     GetComponent<BoxCollider2D>().enabled = false; //this line seems to be causing the problem
     rb.gravityScale = 1;
     Destroy (gameObject, 2);
 }
 
 //function that does things I want on collision
 void OnCollisionEnter2D (Collision2D collision) {
     AudioSource.PlayClipAtPoint (clip, gameObject.transform.position, 0.153f);
     hitCount++;
     GameObject.FindObjectOfType<TextControler>().pointcount();
     
     //if the object hit is c$$anonymous$$zow then it will follow the if statement
     if (gameObject.tag == "c$$anonymous$$zow") {
         makeSmall(0.1f);
         if (hitCount >= (Difficulty.maxHits * 2)) {
             something();
         }
     //if it doesnt have c$$anonymous$$zow tag(therfore it is any other brick) it will do things specified in if condition
     } else {
         makeSmall(0.15f);
         if (hitCount >= Difficulty.maxHits) {
             something();
         }    
     }
     
 }
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

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by EmHuynh · Feb 22, 2016 at 08:15 PM

Hello, @DeathMade2015.

As soon as the ball collides with a brick, the 2DBoxCollider of the brick is disabled. That is how the ball passed through all of the bricks that it collided with. To achieve the result you want, you will need to use function OnCollisionExit2D. In that function, for the brick that the ball collided with, disable its 2DBoxCollider and then set its rigidbody2D isKinematic variable to false.

Comment
royaltrux
mihai2501
firecat91

People who like this

3 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 DeathMade2015 · Feb 22, 2016 at 09:45 PM 0
Share

OMG! I feel so stupid because I'm using OnCollisionExit2D already in different place! That makes sense. Thanks a lot!

Also can you explain why it works fine in Unity 4.6 and not in Unity 5.3? Thanks :)

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

how do i collide and kill the enemy while pressing space Unity 5 C# 2 Answers

uNet hitboxes and movement. 0 Answers

Unity physics Help Implementing Real Life Physics in Jumping System C# 2 Answers

Properly attach to a GameObject after collision? 0 Answers

Can't get object to destroy itself on collision. 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