- Home /
RaycastAll - does rigidbody on parent object interfere with hit-responses of child colliders?
I'm using the following bit of code (nearly verbatim from scripting resource). I think I may not be fully understanding the RaycastAll function. I'll have 2-4 "stacked" colliders and the script will show that hits.length = 2,3, or 4. However, I will only ever get the "top" collider's name or tag using the print statement.
function Update () {
var hits : RaycastHit[];
hits = Physics.RaycastAll (camera.main.ScreenPointToRay(Input.mousePosition),Mathf.Infinity);
for (var i=0;i<hits.length;i++)
{
var hit : RaycastHit = hits[i];
print(hit.transform.gameObject.tag);
var renderer = hit.collider.renderer;
if (renderer)
{
renderer.material.color.r += 0.35;
renderer.material.color.g += 0.35;
renderer.material.color.b += 0.35;
}
}
}
Answer by Mike 3 · Jun 12, 2010 at 11:06 AM
As you've mentioned, adding a rigidbody to the parent sets hit.transform references to the object with the rigidbody
Fortunately, it doesn't change hit.collider - this means it'll work just as expected if you change to that
As to whether this should be the case, my guess is yes - transform gives you back the object you're usually interested in when you're responding to raycasts (i.e. the root object of the enemy, etc etc), and collider gives you back the specific collider
Edit:
http://unity3d.com/support/documentation/ScriptReference/Collision-transform.html
All documented, so not a bad guess (Though next time I check the docs sooner)
Interesting - I thought about writing hit.collider in my post, but then thought it would surely be the same. But it makes sense, it is much easier to build one rigidbody with multiple colliders when the returned transform is the one wit the rigidbody.
Answer by fherbst · Jun 11, 2010 at 09:52 PM
Maybe you could use
var output:string;
for(hit in hits)
{
output += hit.transform.gameObject.name + ", ";
}
print(output);
to see if it works.
It would be strange if the loop gets executed correct but not printed.
I tried your test (thanks for the suggestion). If GO1 is my first (topmost or nearest to the camera) gameobject, I get "GO1,GO1,GO1", "GO1,GO1,GO1,GO1".
So, is this a parent/child issue? Somehow I keep grabbing the name of the parent - even though it's the child's collider being hit?
Testing the script for myself, i get 1,2,3 for my objects, which are all parented to each other (3 parented to 2 parented to 1) - Not a clue what you're doing wrong :S
$$anonymous$$ike, could you possible try adding a rigidbody to your "top" or "outermost" game object? I've stripped everything off my parent object. Children's names show up fine with the original script. Adding a rigidbody to the parent, however, results in the original problem. Not sure why this would be.
yeah, stripped all scripts, bells and whistles off all objects. Isolating the "rigidbody" on/off the parent object seems to be the source. Should I pose this as a new question?
Answer by danielx9 · Oct 21, 2017 at 08:30 PM
I had the same issue and turns out the child's rigidbody was not being simulated, didn't even had to change the code, I just turned simulated on in the game object's inspector and it worked.