- Home /
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?
Have you tried
c.thisCollider.gameObject.renderer.material.color
Have you tried to change the Collision Detection of the rigidbody to continuous?
Thanks for the suggestions but neither of these will fix the problem
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.
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?
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.
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?
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
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
Follow this Question
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