[CLOSED]RaycastAll find closest hit

I have a fps controller and I want to send a RaycastAll which returns the closest hit that is not us.

I know that:

  1. a Raycast doesn’t stop at the first hit point
  2. how to send a raycast

I just don’t understand how to loop through all of the hit points and find the closest one that is not us.

I’ve already asked this question once, but they said that I didn’t give a specific question.

I’ve checked the docs, but that is really hard to understand. So if someone could translate it into kindergarten language that would be great! :slight_smile:

If you just want the object hit first, then just use Raycast(). It is RaycastAll() that collects all the hits and where you cannot depend on the order of the hits in the returns array. Raycast() stops with the first/front collider hit. Unless you have things setup strangely, a Raycast() should not hit the collider it originates from. Colliders are one-sided, and a Raycast() is typically from the inside of a collider (and therefore does not see see the collider). Here is a basic Raycast that might be used on a FPS:

var hit : RaycastHit;        
if (Physics.Raycast (transform.position, transform.forward, hit)) {
    Debug.Log("The ray hit something");

If you need to process a number of objects and use RaycastAll() give me the context and I (or someone) can give you a bit of sample code to help you.

Okay, so i edited for the asker’s request and put in robertbu’s nice and right remark

The RaycastAll function will give back an array of RaycastHit objects.

RaycastHit[] hits;
            hits = Physics.RaycastAll(yourPosition, YourDirection, yourMaxDistance);
//"shoot" a ray from the 1st parameter in the direction of the 2nd parameter and check for maximum the 3rd parameter distance. Objects that are further than the 3rd parameter are not going to be recognized

Then you can iterate through this array and find the min distance.
Of course you cant do this if your hits array hasnt got any element so check it before the min search

if(hits.Length > 0) //if no object was found there is no minimum
  if(!(hits.Length == 1 && hits[0].transform == gameObject.transform)) //if we found only 1 and that is the player object there is also no minimum. This can be written in a simplified version but this is more understandable i think.
    float min = hits[0].distance; //lets assume that the minimum is at the 0th place
    int minIndex = 0; //store the index of the minimum because thats hoow we can find our object
    for(int i = 1; i < hits.Length; ++i)// iterate from the 1st element to the last.(Note that we ignore the 0th element)
       if(hits_.transform != gameObject.transform && hits*.distance < min) //if we found smaller distance and its not the player we got a new minimum*_

min = hits*.distance; //refresh the minimum distance value*
minIndex = i; //refresh the distance
And you got your gameObject at this point by hits[minIndex].collider.gameObject; //get our gameObject with the help of the index.