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. Dismiss Notice

Line of Sight Collider

Discussion in 'Scripting' started by RaGEn, Feb 22, 2016.

  1. RaGEn

    RaGEn

    Joined:
    Feb 3, 2015
    Posts:
    88
    So basically, I need to create a line of sight for my AI for my game to see if they can hit the target or not. I currently have a collider that will determine if there is something blocking the AI's view.

    However I would like to have this colldier change it's size based on the different between the AI's location and it's current target. I was going to use Vector3.Distance to achieve this, however I'm not sure I how can use the numbers it provides me to plug into the collider.

    Anyone have any suggestions?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    raycasts are pretty much designed for this... use physics.overlapshere to detect all the detectable objects "within range" then raycast to them to see if anything is in the way. If you want to limit the field of view you can then check the angle between the raycast and the AI's forward vector.


    Also might be of interest:
     
    RaGEn likes this.
  3. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I would go with linecast over raycast since youre doing los.
     
    RaGEn likes this.
  4. RaGEn

    RaGEn

    Joined:
    Feb 3, 2015
    Posts:
    88
    Thank you that did the trick.
     
  5. RaGEn

    RaGEn

    Joined:
    Feb 3, 2015
    Posts:
    88
    Thank you for your advice and the video helped