Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by NAKAS · Jul 26, 2014 at 03:35 AM · c#2dcollisionrigidbody

2d collision detecting simultaneous collisions.

I've created a simple 2d tennis game. The game detects collision from the ball. If the ball hits the paddle it plays a sound and determines the direction that the ball will be sent to the other side. When a ball hits both the upper boundary and the paddle at the same time no collision is detected. Any insight on how I can get the collider to work?

I included some code and video. In the video the first hit does not fire off the "if ((col.gameObject.tag == "paddle"" when it hits the paddle and the wall at the same time. http://youtu.be/uxta9CxOBJs

 void OnCollisionEnter2D(Collision2D col ) {
         foreach(ContactPoint2D contact in col.contacts){
             Debug.Log("col");
         // Hit the left Racket?
             if ((col.gameObject.tag == "paddle" && noman == false) {
             audio.Play();
             if (noscore == false){
             player1Score++;
                 noscore = true;
             }
 
Comment
Add comment
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

· Add your reply
  • Sort: 
avatar image
2

Answer by SilentSin · Jul 26, 2014 at 05:12 AM

The problem is that you are using "col.gameObject.tag" instead of "contact".

The ball hits the paddle and wall, so it gets a collision message with 2 contacts. You loop through both contacts, but check col.gameObject.tag for both of them rather than checking the tag of the contact point.

What you want is if (contact.gameObject.tag

Also, you have 2 opening brackets after the if, but only one closing bracket.

Comment
Add comment · Show 2 · 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 NAKAS · Jul 26, 2014 at 08:18 PM 0
Share

Hey thanks a lot. This was a big step in the right direction. what was strange was it only returned 1 contact even though it collided with both objects. This seems like a glitch. I was able to solve it by using contact.point to work if it hits the wall. $$anonymous$$ind of crude but it worked. I'm going to build an example project and bring this issue up in the forums soon.

avatar image SilentSin · Jul 27, 2014 at 12:09 AM 0
Share

I'm not sure about 2D, but in 3D you would get a separate OnCollisionEnter call for every object you hit, where each one of them would have the contact point or points for that object. So if you overlap two parts of a terrain as well as a building, you would get 2 calls; one with the terrain's 2 contact points and the other with just 1 contact point for the building.

If my answer solved your problem, you should mark it as correct.

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.

Follow this Question

Answers Answers and Comments

24 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

rigidbody2D.addforce in collider 1 Answer

How to detect in grid of objects if one is destroyed while beside adjacent objects without rigidbody? 1 Answer

C# OnCollisionEnter2D Not Occuring 1 Answer

Object following a path and colliding with other objects with physics. 1 Answer

Any way to ignore collision between rigidbodies and colliders/character controllers? 1 Answer


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