Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Sorted Raycast

Discussion in 'Scripting' started by sidestepper, Sep 27, 2009.

  1. sidestepper

    sidestepper

    Joined:
    Oct 2, 2008
    Posts:
    96
    Quick question, couldn't find anything on it in forums. When I use RaycastAll and get my array back, is there a known system to what order that array is in?

    Reason being: I have an ai who determines if he has a clean shot or not before shooting. So what he does is cast a ray through everything and get the list back, the reason the sort matters is because lets say the ray was cast 100 meters, in that distance it would intersect the player and the terrain, but the player is closer than the impact point on the terrain so technically it would be a clean shot, could i be guaranteed that hits[0] is the player then hits[1] is the terrain? Or is the order unknown?
     
  2. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    Not sure about the sorting of the array, but why not use Physics.Raycast instead? Then you only get the first hit - if it's the player then you have a clear shot, otherwise not.

    /P
     
  3. sidestepper

    sidestepper

    Joined:
    Oct 2, 2008
    Posts:
    96
    Yes I realized it after I made the post and ended up doing this in a better way. But I am still curious if the RaycastAll is sorted - I may have to tinker with this in a test project - but so busy. If I get around to figuring this out I will post back here.
     
  4. Barbur

    Barbur

    Joined:
    Oct 30, 2009
    Posts:
    160
    The HitInfos array goes from back to front :)
     
  5. sidestepper

    sidestepper

    Joined:
    Oct 2, 2008
    Posts:
    96
    Awesomesauce - not really on that anymore, but still good to know, thanks for taking the time to figure that out.