- Home /
 
 
               Question by 
               TwitchBlade · Sep 02, 2014 at 07:57 AM · 
                raycastchild object  
              
 
              how to find the child of an object i hit with raycast
Like the title says i need to find the children of an object i hit with a raycast so far i have
 float tempDist = 0f;
                             float prevDist = 0f;
                             foreach (Transform child in transform)
                             {
                                 if(tempDist != null)
                                 {
                                     prevDist = tempDist;
                                 }
                                 tempDist = Vector3.Distance(hit.point, child.transform.position);
                                 if(tempDist > prevDist)
                                 {
                                     furthestAway = child.collider.name;
                                 }
                                 if(child.collider.name == "posXLock")
                                 {
                                     posX = child.transform.position;
                                 }
                                 else if(child.collider.name == "negXLock")
                                 {
                                     negX = child.transform.position;
                                 }
                                 else if(child.collider.name == "posZLock")
                                 {
                                     posZ = child.transform.position;
                                 }
                                 else if(child.collider.name == "negZLock")
                                 {
                                     negZ = child.transform.position;
                                 }
                             }
 
               but that returns the object the script is on how do i make it do the one i hit with a raycast
               Comment
              
 
               
              Answer by robertbu · Sep 02, 2014 at 07:58 AM
You did not include your Raycast() code here. Assuming that 'hit' is the RaycastHit returned by the Raycast(), just do:
  foreach (Transform child in hit.transform)
 
              Your answer
 
             Follow this Question
Related Questions
Instantiate On Container Child 1 Answer
Bouncing LineRenderer, null exception. 0 Answers
Look At Player with Clamping. 1 Answer
Weird problems with exectution orders and just simple bools???? 1 Answer