Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Checking line of sight on path

Discussion in 'Scripting' started by joshcamas, Jun 16, 2018.

  1. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,276
    Hello friends :)

    On my AI, I have the npc have several abilities - mainly melee and ranged. (And others)
    These abilities can be intermixed, so the way the AI chooses which ability to use is to calculate a "cost" variable... the smaller cost = the better choice.

    Anyways, with my melee weapon, cost is easy to calculate - simply return the distance from the target. But with ranged, it's not so easy. When an NPC is in ranged mode, and is not in line of sight, it will pathfind towards its target, until its in range / in LOS. Due to this, there's no easy way of learning how long it will take to finally get in range.

    The only idea I have is to calculate the path, then step through it, perhaps every 1-2 units, sending raycasts until one is LOS.

    And I guess a cheap very cheap option would be simply "if NPC is X units away, always use bow".

    Thoughts?

    Josh
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    My question would be why you would want to know when LOS will be available? Is the scenario that the archers have X-Ray vision? They can see people through obstacles to know when LOS will be available?

    Wouldn't that just be the default and entirely natural choice? :)

    Surely logic is something along the lines of :
    1. If NPC can't see target, try to find it (somehow).
    2. If NPC can see target, is target near enough for melee weapon? If yes, use melee attack.
    3. If NPC can see target, is target near enough for ranged weapon? If yes, use ranged attack.
    4. If NPC can see target but is out of range, move towards target.