- Home /
Presence of rigidbody on parent stops OnMouseEnter in a child collider
I'm working on a simple 3D tooltip that uses a box collider. But the hierarchy containing the tooltip gets parented to an object with a rigid body. This kills the OnMouseOver
and OnMouseEnter
events for my child collider (Though, Update
functions in the child's script still work fine). If I Destroy
the rigid body on the parent, OnMouseOver
on my child comes back to life. Tried disabling various properties of the rigidbody. Didn't help.
Is there a way to work around this? Is there a better way to do this?
var card : GameObject;
function Start () { card.SetActiveRecursively(false);
}
//this part works after parenting - using it to verify script is working
function Update () { if (Input.GetKeyDown ("o")) { card.SetActiveRecursively(true); } if (Input.GetKeyDown("i")) { card.SetActiveRecursively(false);
}
}
//this part stops working after parenting function OnMouseEnter () { //activate the card Debug.Log("MouseEnter"); card.SetActiveRecursively(true); }
function OnMouseExit () { //deactivate the card Debug.Log("MouseExit");
card.SetActiveRecursively(false);
}
Answer by laurent · Mar 23, 2011 at 05:29 AM
RigidBody swallow all child collider event. Add a rigid to your tooltip and switch it to kinematic. Or if you don't want to burden the physics with extra rb, add a script to your existing rigid
void OnMouseEnter()
{
BroadcastMessahe("OnMouseEnter");
}
Hey this worked!! Added a kinematic rigidbody to the child object that had the tooltip and Voila!
Thanks very much!
@laurent: I like ask how would adding rigids to the child colliders burden the physics? If say the parameters (such as mass) of these 'new' rb are reduced to $$anonymous$$imum, they will not affect the overall physics, right?
@Nik maybe this should be its own question. Would be good to have that informatation.
"GameObject.Broadcast$$anonymous$$essage: Calls the method named methodName on every $$anonymous$$onoBehaviour in this game object or any of its children." - wouldn't broadcasting the message create an infinite loop with itself?
Yes it would. To only call it on the children I added this method. The option of adding a dummy rb worked too, yet disabled the options to collide in my code somehow.
// Rigidbody swallows all mouse events. So to transfer the On$$anonymous$$ouseDown event (others to be added when necessary) downwards, add a broadcast to all childs
// Alternative is to add a dummy RigidBody (kinematic = checked) on the objects needing to receive mouse events, but this disables the collision effects somehow
public void On$$anonymous$$ouseDown()
{
var children = gameObject.GetComponentsInChildren<Transform>();
foreach (var child in children)
{
if (child.transform != this.transform)
child.gameObject.Broadcast$$anonymous$$essage("On$$anonymous$$ouseDown", options: Send$$anonymous$$essageOptions.DontRequireReceiver);
}
}
Answer by Tom-Mensink · Sep 02, 2018 at 10:26 AM
Adding this to the highest level with the rigidbody attached works. You can replace OnMouseDown with OnMouseEnter of course.
public void OnMouseDown()
{
var children = gameObject.GetComponentsInChildren<Transform>();
foreach (var child in children)
{
if (child.transform != this.transform)
child.gameObject.BroadcastMessage("OnMouseDown", options: SendMessageOptions.DontRequireReceiver);
}
}
Your answer
Follow this Question
Related Questions
Connecting Multiple Rigidbodies with Collision 0 Answers
Mesh renderer does not move with parent rigid body 5 Answers
Collision Detection for Kinematic Rigidbodies 0 Answers
Moving a player on a rigidbody platform that can collide 2 Answers
Guidelines for using rigidbody, collider, CharacterControllerScript, etc? 3 Answers