Search Unity

Raycast on child gives parent name in hit.transform.name

Discussion in 'iOS and tvOS' started by chishti, Aug 12, 2010.

  1. chishti

    chishti

    Joined:
    May 21, 2009
    Posts:
    188
    Hi I cast ray like this.
    I have to ignore the layer number 8

    Code (csharp):
    1.  
    2. var layerMask = 1 << 8;
    3. layerMask = ~layerMask;
    4.                
    5. var ray = Camera.main.ScreenPointToRay(touch.position);
    6. var hit : RaycastHit;
    7. if(Physics.Raycast(ray, hit, 100, layerMask))
    8. {
    9.     print(hit.transform.name);
    10.     if(hit.transform.name == "ChildObjName")
    11.     {
    12.         print("yes child obj");
    13.  
    14.     }
    15. }
    16.  
    I have assigned the Layer number 8 to Parent Object and I need to cast ray on its child object. but when tap on child object it always gives me parent object name. but i have assigned IgnoreLayer(layer no. 8 ) to parent object.

    Also I can tap the other objects that are not child of this parent object.?

    Any thing that I can do to detect it?

    thanks in advance
     
  2. chishti

    chishti

    Joined:
    May 21, 2009
    Posts:
    188
    if someone is unable to understand. I can explain.
     
  3. Bulgroz

    Bulgroz

    Joined:
    Oct 19, 2007
    Posts:
    49
    Well, could you not have the child object in a layer that is not layer 8 and then detect the child directly via the raycast ?

    Do you have to go via the parent ? Sounds like you are trying to cross the river to get water.

    For sure ignoring a layer is a bad idea if you want to detect someting in it.



    /Chris
     
  4. chishti

    chishti

    Joined:
    May 21, 2009
    Posts:
    188
    No I need to access the child directly.
    the child object is away from parent. I am making the object child due to i have to keep it with parent in complete game play.

    If ignoring layer is a bad idea than what could be the best way to do it?
     
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Colliders on child objects are treated as though they are additions to the parent collider (this is what you want for most physics tasks). However, the RaycastHit object also has a field for the collider that was hit. The reported transform is the parent's transform but the collider is the exact collider that was hit. If you need the transform of the child object, you can use the collider's transform property, etc.
     
  6. chishti

    chishti

    Joined:
    May 21, 2009
    Posts:
    188
    thanks andeeee it works
     
    Westland likes this.
  7. jstapleton

    jstapleton

    Joined:
    Apr 23, 2014
    Posts:
    1
    Works great! Thanks andeeeee :D
     
  8. Legion555

    Legion555

    Joined:
    Mar 22, 2014
    Posts:
    4
    thanks andeeee it works!!
     
  9. aheredia

    aheredia

    Joined:
    Dec 10, 2015
    Posts:
    1
    Thanks!!!
     
  10. loliconest

    loliconest

    Joined:
    Sep 21, 2015
    Posts:
    31
    OMG saved my life!
     
  11. junmin4

    junmin4

    Joined:
    Oct 9, 2016
    Posts:
    2
    THANK YOU, ANDEEEEEE!!!
     
  12. klasclaywood

    klasclaywood

    Joined:
    Mar 3, 2017
    Posts:
    1
    Oh my goodness, thank you so much! This finally explains why I couldn't get the raycast to ignore the parent object! Andeee you're my hero!
     
  13. Baumkuchen

    Baumkuchen

    Joined:
    Feb 14, 2016
    Posts:
    8
    Good to know!
     
  14. cmz-neu4590

    cmz-neu4590

    Joined:
    Apr 14, 2016
    Posts:
    7
    Thanks Andeee!
     
  15. AGSMarcinDraszczuk

    AGSMarcinDraszczuk

    Joined:
    Mar 31, 2015
    Posts:
    30
    Thanks andeeeee!
     
  16. ayushsingla991

    ayushsingla991

    Joined:
    Oct 1, 2016
    Posts:
    14
    Thanks Andeeeeeeeeeeeeeee!
     
  17. jvhgamer

    jvhgamer

    Joined:
    Jun 27, 2017
    Posts:
    36
    Thanks andeeeee
     
  18. airzocker

    airzocker

    Joined:
    Mar 13, 2018
    Posts:
    1
    just searched for this and andeeeee had the perfect answer ;D
     
  19. Unsui

    Unsui

    Joined:
    Oct 10, 2017
    Posts:
    4
    It helps! Thank you
     
  20. th3z0d1ac

    th3z0d1ac

    Joined:
    May 14, 2014
    Posts:
    15
    Dude thanks!
     
  21. nanditho

    nanditho

    Joined:
    Jan 10, 2018
    Posts:
    27

    Can you explain this, I'm new to unity, i habe similar problem.

    Collision detected that my player keep hit parent object even though player is on the top of child object of that parent object.

    So i got log saying player hit child object while the player hit the child object.

    I'm so confused.

    Thank in advance guys
     
  22. nanditho

    nanditho

    Joined:
    Jan 10, 2018
    Posts:
    27
    Can you explain it.
    I'm new to unity.

    I have kind of similar problem.
    OnCollisionEnter function keep telling me that my sphere player hits empty parent object while the player bumps on to child object (the child object transform position is quite far from the parent object).

    So, i can not check the material color of my child object if my player collider can not detect collider with my child object.

    Please kindly advise.
    Thank in advance
     
  23. nanditho

    nanditho

    Joined:
    Jan 10, 2018
    Posts:
    27
    Would you mind if you could explain this to me what Andee said.

    I have OnCollisionEnter keep telling me that my Player hits parent obkect even though it hits the child object not the parent (position of child quite far from parent object).

    So, what is wrong with this?

    I want to detect wheter my player hit a child object with similar color to my player object.

    Thanks in advance thanks
     
  24. nanditho

    nanditho

    Joined:
    Jan 10, 2018
    Posts:
    27
    Would you mind if you could explain this to me what Andee said.

    I have OnCollisionEnter keep telling me that my Player hits parent obkect even though it hits the child object not the parent (position of child quite far from parent object).

    So, what is wrong with this?

    I want to detect wheter my player hit a child object with similar color to my player object.
     
  25. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    YES.

    Code (CSharp):
    1. hit.transform.gameObject
    2.  
    3. hit.collider.transform.gameObject
    Turns out these are different things! 90 minutes to learn this :D
     
    foggyfields and LordHeman like this.
  26. Westland

    Westland

    Joined:
    Jan 26, 2015
    Posts:
    27
    It wokkens, thanks andeeeee and chishti for your perseverance.

    Everywhere I go, I see his name.
     
  27. unity_TaMhDW8pKzMI7g

    unity_TaMhDW8pKzMI7g

    Joined:
    Feb 10, 2020
    Posts:
    1
    your are de best andeeeee
     
  28. freddajiy

    freddajiy

    Joined:
    Aug 24, 2020
    Posts:
    1
    wow, it works! Thanks andeeeeeeeeeeeeeeeeee
     
  29. marcospgp

    marcospgp

    Joined:
    Jun 11, 2018
    Posts:
    194
    Such a silly decision to make hit.transform return the parent if it has a rigidbody! APIs should be predictable
     
    shoopi, AngryAndrew, dencaval and 4 others like this.
  30. OliverMOP

    OliverMOP

    Joined:
    Feb 9, 2021
    Posts:
    1
    You are the master
     
  31. Cubemann

    Cubemann

    Joined:
    Oct 22, 2020
    Posts:
    4
    Wha? Can you please explain a bit more?
     
  32. Leohd

    Leohd

    Joined:
    Feb 7, 2017
    Posts:
    32
    When you Raycast, hit.transform returns the parent's transform of the hit, and hit.collider returns the collider of the hit.

    This is very illogical, it should be standardized so both hit.transform and hit.collider refer to the components of hit.
     
    Westland likes this.
  33. Ex6tra

    Ex6tra

    Joined:
    Oct 31, 2019
    Posts:
    14
    Use "hit.collider.transform.name" instead of "hit.transform.name"
     
    Westland likes this.
  34. S_Bezrukov

    S_Bezrukov

    Joined:
    Jul 30, 2020
    Posts:
    3
    Thank you, Andeeee!!!
     
  35. S_Bezrukov

    S_Bezrukov

    Joined:
    Jul 30, 2020
    Posts:
    3
    Yeah, this strange decision made me nervous for a couple of hours
     
    Leohd likes this.
  36. andikaakun17

    andikaakun17

    Joined:
    Dec 9, 2019
    Posts:
    3
    Thank you andeeee !
     
  37. JampotDev

    JampotDev

    Joined:
    May 12, 2021
    Posts:
    9
    12 years later, but thank you so much Andeeee! This issue has caused quite some mental breakdowns!
     
    Westland likes this.
  38. Decreationist

    Decreationist

    Joined:
    Nov 15, 2016
    Posts:
    4
    Still going strong, thanks Andeeeeee.

    Also, @Unity please F***ing fix this, or atleast document it better.
     
  39. lingARApp

    lingARApp

    Joined:
    Oct 26, 2019
    Posts:
    2
    It works, thanks andeeeee
     
  40. yosimba2000

    yosimba2000

    Joined:
    Jun 3, 2021
    Posts:
    25
    Still relevant in 2022, thanks andeeeeee! Also FYI, he was last seen on the forums in 2015, so 7 years ago. Imagine if he came back to all these thank yous lol
     
  41. rxmarccall

    rxmarccall

    Joined:
    Oct 13, 2011
    Posts:
    353
    Thanks Andeeeeee
     
  42. spilat12

    spilat12

    Joined:
    Feb 10, 2015
    Posts:
    38
    Hot darn, dude! Thanks, this is such helpful info!
     
  43. Kasidit0720

    Kasidit0720

    Joined:
    May 10, 2021
    Posts:
    1
    Thanks Andeeeeee
     
  44. postponedtonever

    postponedtonever

    Joined:
    Jan 12, 2023
    Posts:
    1
    Thank you so much Andeeeeee this helped immensely! Absolute legend
     
  45. ggt3944

    ggt3944

    Joined:
    Jul 13, 2021
    Posts:
    1
    Thanks Andeeeeeeee
     
  46. foggyfields

    foggyfields

    Joined:
    Apr 1, 2019
    Posts:
    2
    Thanks Andeeeee!!! Was strugglin...all over one word. collider.
     
  47. OverZhorman

    OverZhorman

    Joined:
    Mar 3, 2021
    Posts:
    4
    Thank you andreeeeee. Unity making this stupid feature was prob. thinking: "Oh! Why not add this counterintuitive garbage nobody asked for! I bet they don`t know how to find collider`s parent on their own! Let`s also mention it only once, or, better: not mention it at all!"