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 MDurkin · Oct 17, 2013 at 01:17 PM · c#collisionrigidbodyoncollisionentercollision event

Further collision beyond the OnCollisionEnter() event

I am creating a game where I need collision between blocks of the same colour to be acted upon. The blocks are children of gameobjects which act to create clusters of blocks - kind of like Tetris shapes. The rigidbody component thus belongs to the parent gameobject and OnCollisionEnter is called only when two parents collide. I have no problem identifying when matching blocks collide when OnCollisionEnter is called. Here is the code I use for that:

 void OnCollisionEnter(Collision collision)
     {
         foreach (ContactPoint c in collision.contacts)
         {
             if ((c.thisCollider.renderer.material.color == c.otherCollider.renderer.material.color)
                 && (c.thisCollider.renderer.material.color != Color.gray)
                 && (c.otherCollider.renderer.material.color != Color.gray))
             {
                 ScorePoint = true;
                 c.thisCollider.renderer.material.color = Color.gray;
                 c.otherCollider.renderer.material.color = Color.gray;
             }
         }


(the blocks are coloured gray after colliding with the same colour)

The actual problem I have is illustrated below. In the first frame the gameobjects collide and no matching colours are detected. The collision between the gameobjects holds until two matching coloured blocks are touching, but of course OnCollisionEnter will not be called. I have tried playing with the OnCollisionStay event but I haven't managed to do so without raising huge performance issues on mobile devices.

If anyone has any advice it would be greatly appreciated. Perhaps a solution which works only in this special case where all the individual blocks are cubes?

alt text

qu_example.png (3.7 kB)
Comment
Add comment · Show 5
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 meat5000 ♦ · Oct 17, 2013 at 01:24 PM 0
Share

Have you tried

 c.thisCollider.gameObject.renderer.material.color
avatar image ZaidesA · Oct 17, 2013 at 03:09 PM 0
Share

Have you tried to change the Collision Detection of the rigidbody to continuous?

avatar image MDurkin · Oct 17, 2013 at 04:43 PM 0
Share

Thanks for the suggestions but neither of these will fix the problem

avatar image mattmanj17 · Oct 18, 2013 at 02:27 AM 0
Share

Do you want to change only the color of the two blocks that collide, or change the entire cluster when one of its blocks collides with a block of the same color?

Have you tried moving the script to each child block ins$$anonymous$$d of the whole parent cluster? That way OnCollisionEnter would be called for each block.

All the child objects could still be parented to another game object so that if you specifically want to change ALL the blocks in a cluster to grey when one of them collides, you could access each child block from the cluster parent object.

avatar image MDurkin · Oct 18, 2013 at 10:56 AM 0
Share

Only the two colliding blocks. The rigidbody nature of the parent stops OnCollisionEnter being called for the individual colliders... I think it forms a compound collider. $$anonymous$$now a work around?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Huacanacha · Oct 18, 2013 at 02:56 AM

It seems like all your problems would be solved if each cube had it's own collider. Then you can just test each cube-cube collision for matching colors in OnCollisionEnter, rather than testing each child of the parent via the material at each collision point (which you are doing via the material of the collision points). You can still nest the cubes under a parent object so they move together, and you can still access the parent from the child cubes if necessary etc.

This should also perform somewhat better when processing the collisions as you don't have to test each collision point.

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 MDurkin · Oct 18, 2013 at 10:09 AM 0
Share

Thank you for your reply. Everything I've read online seems to suggest that the rigidbody component of the parent 'swallows' the colliders of the children, so that OnCollisionEnter is only called for the whole parent object. Do you know of any way around this?

avatar image mattmanj17 · Oct 18, 2013 at 03:18 PM 0
Share

as long as the script is attached to each individual block, the parenting shouldn't matter. and if you parent all the individual blocks to an empty game object, then the parent wont even have a collider. as far as parent colliders "swallowing" child colliders, are you referring to this

Compound Colliders

in that case, as long as the script is attached to the child objects, and not the parent, and each child has its own ridged body, each child should still behave as an individual collider.

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

18 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

Related Questions

Transform collider not detecting collision on rigidbody collider 2 Answers

Collision detection from specific directions using rigid body character? 0 Answers

Objects with colliders going through walls and each other. 3 Answers

Prevent a specific RB to influence another, but still collide with everything 0 Answers

Force a script generated primitive to test for collision 2 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