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.
     
    BenVenNL, vladibo, Hukha and 18 others like this.
  6. chishti

    chishti

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

    jstapleton

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

    Legion555

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

    aheredia

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

    loliconest

    Joined:
    Sep 21, 2015
    Posts:
    27
    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:
    21
    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:
    3
    It helps! Thank you
     
  20. th3z0d1ac

    th3z0d1ac

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

    nanditho

    Joined:
    Jan 10, 2018
    Posts:
    10

    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:
    10
    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:
    10
    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:
    10
    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:
    2,973
    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