Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Raycast no longer hitting, if parent has a Rigidbody component

Discussion in 'Scripting' started by yahodahan, Feb 27, 2011.

  1. yahodahan

    yahodahan

    Joined:
    Apr 26, 2009
    Posts:
    1,379
    **Edit: please ignore my silly chamfered box collisions...just testing some theories ;)**

    Testing a little idea out, and hitting another issue- I have a center, visible, cube with a same-size cube attached to each of it's faces. Each of the attached cubes is invisible, but on raycast hit, become visible, and then i use their placement to spawn another cube at that position, on mouse click.



    Problem I'm finding is, this only works if the parent cube (center one) does not have a rigidbody attached. The collision boxes are not overlapping, and I can't think of what else might cause this. Stumped, again...

    Trying some other techniques, bleeding time on this currently. Meanwhile, if someone has any advice, would be glad to hear it :) Thanks!
     
    Last edited: Feb 27, 2011
  2. yahodahan

    yahodahan

    Joined:
    Apr 26, 2009
    Posts:
    1,379
    Seems it will only hit the top-level-parent...:(
     
    Last edited: Feb 27, 2011
  3. yahodahan

    yahodahan

    Joined:
    Apr 26, 2009
    Posts:
    1,379
    works when parented to empty gameobject. breaks as soon as add rigidbody to that parent.
    works if rigidbody is added to the "outercubes"- the issue is purely if the the parent has rigidbody. :(
     
  4. yahodahan

    yahodahan

    Joined:
    Apr 26, 2009
    Posts:
    1,379
    In case anyone was watching this thread, looking for a solution also- I never found one. I did, however, figure out a quick and dirty workaround- make the outer cubes a seperate group, not parented to the inner cube (which has the rigidbody). I then used a quick script on the center cube, to keep the outer cubes at exactly it's position and rotation, updated each frame. Not exactly efficient, but works for now! :)
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    collision and raycast are designed to allow you compound colliders.
    as such if you raycast or collide with something, the collision event will be sent up the hierarchy to the first found rigidbody (or better to its GO in case of collision trigger events). only if no rigidbody is present, (or if the object hit had a rigidbody) it will be handled by the object that was hit

    I guess that this was not taken into consideration here, is that possible?
     
  6. yahodahan

    yahodahan

    Joined:
    Apr 26, 2009
    Posts:
    1,379
    Aha, yes, I was beginning to assume something like that was at work here...luckily, I did find a workaround, but it's not a very good one. Is there no way to "get" just that object which was hit, not the parent rigidbody?

    thanks!
     
  7. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    What if you put the invisible boxes on a separate layer then use a layermask in your picking raycast?
     
  8. yahodahan

    yahodahan

    Joined:
    Apr 26, 2009
    Posts:
    1,379
    hmm, that's a good idea...but, like Dreamora said, won't it still just traverse up the hierarchy and grab the top level rigidbody? I'll give it a try in a bit, just in case. thanks!
     
  9. ev3d

    ev3d

    Joined:
    Apr 19, 2013
    Posts:
    327
    I am having this same issue... since this is old, is there now a fix?
     
  10. midnightcrawler1

    midnightcrawler1

    Joined:
    Jan 17, 2017
    Posts:
    15
    dude! you saved me hours of frustration by figuring this out. I had a nested rigidbody inside a parent and was killing me for hours trying to figure out why raycast hits are in the wrong position, until I read this and removed the parent rigidbody (which incidentally I didn't even need, not sure why it was there) and boom, everything started working like a charm.
    Thanks.
     
  11. Xhitman

    Xhitman

    Joined:
    Oct 30, 2015
    Posts:
    452
    I just face the same problem, wonder why raycast fail when adding rigidbody to parent

    Parent [Rigidbody, w/o collider]
    child [ collider]

    Shouldn't it be fixed?
     
  12. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,194
    Ancient thread. Also no, see "Compound Colliders" on this page.

    If you want to have a collider child of a rigidbody where the child is treated as an independent collider instead of as a part of the rigidbody, you add a rigidbody to the child. Usually you want to set that to kinematic so the physics engine doesn't get confused.
     
    dcmrobin likes this.
  13. Xhitman

    Xhitman

    Joined:
    Oct 30, 2015
    Posts:
    452
    But the script is on parent, and i want to use OnTriggerEnter() on parent's script.

    I know I can add another script on child, but is there any simple solution? In particular, I have hundred such type objects in scene.
     
  14. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,194
    OnTriggerEnter should fire on the parent with the setup you're describing, if the conditions for that method firing is in place. See the bottom of this page.

    But, yeah, don't necro a half-releated thread from 2011 to ask about this, create a new one.
     
  15. dcmrobin

    dcmrobin

    Joined:
    Feb 15, 2021
    Posts:
    7
    This fixed my problem! Thankyou so much!!