- Home /
Detect onMouseDown() on children colliders of a rigidbody parent
So I have been having trouble with detecting OnMouseDown() on the child object when parented to a rigidbody. I found out that rigidbodies make a combined collider with all it's children so when clicking, it detects the empty game object and not the individual child objects. Is there a way to detect which child collider was hit when hitting the rigidbodies combined collider? Is there a way around this?
Ignore Raycast layer does not change anything and I have tried using a manual ray from screen etc. I have tried hit.collider.name
.
I need to have it setup this way so the parent rigidbody will be affected by physics whilst the children collide with the level and move altogether since they are children. I also need to do this inside 1 script.
Answer by Tyrdle · Oct 01, 2019 at 03:22 AM
So I have found a way to accomplish what I need, instead of parenting the children I just give them a fixed joint that is connected to the empty :)
Answer by starrtennis · Oct 01, 2019 at 01:34 AM
Wait, so you're saying that a RigidBody on a parent object yields OnMouseDown() events detected by the child even if the child doesn't have a RigidBody? That sounds ludicrous. Or do both the parent and the child have a rigidbody? Why do they need to be parent/children in the first place?
I would imagine the OnCollision from the RigidBody would just detect the parent OR the child. But you're saying they're overlapping in space? Then it would only make sense that both of them are detecting collisions. If you want both to have rigid bodies but only one detect collisions, you could set one as a trigger (the one you don't want to collide)--it will then only detect collisions but not act as physics, when a collider ENTERS it, if that makes sense.
Your description is a little ambiguous so if you could maybe explain with some code examples that might help.
Cheers, Mike
Yes apparently rigidbodies will make a combined collider that includes all its childrens.
I have tried many combinations of colliders and rigidbodies and character controllers.
Essentially I am trying to have these 4 squares that makeup 1 big square (2x2) that move in unison, so I attached them all to a parented empty, the children collide and the rigidbody parent applys physics to them all, but clicking on them fires in the parent not the child clicked.
For example
private void On$$anonymous$$ouseDown()
{
Debug.Log(gameObject.name);
}
will just log "Player" the name of my parent empty object and not "Cube" etc. for the child objects.
Your answer
Follow this Question
Related Questions
How to check place on empty for placement polygon Rigidbody2d? 0 Answers
How do i stop rigidbody2D bounce 1 Answer
Is this a correct way to have multiple different compound colliders in one gameobject? 1 Answer
2D physics object acts weird when pressed into other physics object 0 Answers
AddForce to RigidBody2D on the same frame as SetParent(null) causes small jitter 2 Answers