- Home /
[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! :)
Answer by robertbu · Aug 04, 2014 at 07:48 AM
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.
I am not sure that is deter$$anonymous$$istic that which objects that single raycast will give back if it can hit more than one. Are you?
Im just suspicious because the doc is writing that RaycastAll is not giving back the elements in the right order
Raycast stops at the first collider hit. But that could be myself with the standard fps controller, because the main camera is inside the controller. If you look down you hit yourself. I want to make sure that it is not possible to hit yourself by using raycastAll, this is not only because of the game I want to make, but also because I want to learn it!
And no, RaycastAll doesn't return the hit points in the right order, it mixes them up.
Answer by dsada · Aug 04, 2014 at 07:47 AM
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[i].transform != gameObject.transform && hits[i].distance < min) //if we found smaller distance and its not the player we got a new minimum
{
min = hits[i].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.
@$$anonymous$$r$$anonymous$$elonPie - you asked about eli$$anonymous$$ating yourself from this list. I don't believe the 'you' will generate a hit, but if it does, you can change @dsada's example by changing line 6 to:
if(hits[i].transform. != transform && hits[i].distance < $$anonymous$$)
Thanks for the answer both of you! This was really useful @dsada But can you explain a bit more of what you are actually doing? Like maybe in this code comment it out, saying what you are doing in this and this line? that would really be great!
@robertbu, if I have a fps controller, the standard one that comes with unity asset, When you ask its main camera to send a raycast forward, then if you look down, you will hit your own collider.
What kind of collider do you have on your FPS? Typically a capsule collider is used, and I've never heard of a capsule collier getting hit from a raycast from the pivot of the capsule. Do a:
Debug.Log(hit.name+", "+hit.tag);
...inside the raycast to verify what is getting hit. If you have a more complex mesh on the player, or if the player is holding something that the raycast might hit, then there are alternate solutions to solve the problem. One solution is to put your player on its own layer, and use a layer mask to eli$$anonymous$$ate the player. Be careful doing your research with layer masks. They are bitsets, not integers, and it is easy to get them wrong. I$$anonymous$$HO RaycastAll() is not the right solution to fix the probelm. There are other potential solutions to the raycast hitting the player, but I'd need to know what is being hit, its relationship to the player, and how the part that is getting hit interacts with the environment.
Please, avoid that check by ignoring the player layer on the raycast! better performance and less code!
Your answer
Follow this Question
Related Questions
[CLOSED]RaycastAll Help 1 Answer
Bullet Effect (RaycastAll Question) 1 Answer
Check if RaycastAll hits[i].point == null 0 Answers
RaycastAll Detect if nothing hit 1 Answer
Null reference exception on Screenpointtoray (Multiplayer) 1 Answer