- Home /
Child objects trigger collider not working when parent has rigidbody
I have tried using OnMouseOver and a raycast mouse event using camera.screenpointtoray but I still can't get the collider to recognize when my mouse is over it if it's parent object has a rigidbody. What gives? Any suggestions/advice?
Does your parent object have a collider. Raycast will stop at the first collider it hits. If you have some code we might be able to point out why it's not working for you.
Yes it does. In my second attempt I tried using a raycast to check where the mouse is pointing. It's set to ignore layer 8 I set the parent to layer 8 not the children. No this works fine and dandy with no rigidbody attached to the parent but as soon as I attach the rigidbody it no longer works same thing happens with the On$$anonymous$$ouseOver event and no collider attached to the parent.
I think you are experiencing the effect of a compound collider. Which means children colliders of a rigidbody are treated as the rigidbodies collider. At least thats how i under stand them from the documentation.
http://unity3d.com/support/documentation/Components/class-Rigidbody.html
Answer by slkjdfv · Apr 21, 2012 at 02:36 PM
I solved it last night. It turns out OnMouseOver events will NOT not work on child objects if the root parent has a rigidbody. BUT! If you use a raycast and hit.collider.transform it does. Here's the script I used to test this(It's in C#) :
Using UnityEngine;
Using System.Collections;
public Class AdvancedMouseDetect: MonoBehavior
{
public Camera myCam;
private LayerMask _layerMask 1 >> 8; //This sets the object to ignore layer 8 which is a custom layer;
void GetMouseInfo()
{
Ray ray = myCam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray,out hit, Mathf.Infinity, _layerMask))
{
if(hit.collider.transform.name == name)//This detects the colliders transform if you just use hit.transform it gets the root parent at least thats what I found.
{
Debug.Log("Mouse is over " + name + ".")
}
}
}
void Update()
{
GetMouseInfo();
}
}
I hope this helps someone or every one :D
I LOVE YOU. I was having the same problem and using hit.collider.transform ins$$anonymous$$d of hit.transform fixed it.
Thanks man...
Unity support. Can you fix your documentation if that's not a bug (i guess its not bug cause of 2 years appearacne) : https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.On$$anonymous$$ouseOver.html ?
Probably not, cause of particle system, that is the only target of this engine recently.