Search Unity

Finding a location where archer can shoot player

Discussion in 'Navigation' started by FeastSC2, Nov 13, 2019.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    In order for my archer to shoot the player he has to have a clear line of sight of the player.
    I was wondering if there is a way to find a location from which he can shoot at the player by using Unity's NavMesh system in combination with raycasts, or maybe some other method.

    What are the ways this can be done?
     
  2. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    I calculate concentric rings around my NPCs target. I find locations left and right 15 degrees of the current angle, then back 5 yards and forwards 5 yards depending on if they are in the right distance range. I then see first if the position can see the target with a raycast then navmesh.samplepositon to see if the position is even possible to reach. I also do a raycast between the two points to be sure there are no obstructions. This causes the ai to always be circle strafing while testing to keep line of sight. If there are no available positions, the nav defaults to the target position to make it follow the player.
     
    FeastSC2 and DwinTeimlon like this.
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978

    Thanks that's a great idea.

    I've been trying to implement it but I ran into a problem when it came to knowing if the agent can go to to the point on the navmesh.

    You mentioned using NavMeshAgent.Raycast(): The raycast provides the information if there is something that blocks the path linearly to the destination. That's not what I want, I want to know if there is a way, even if it's not linear, to get to the destination.
    or using NavMeshAgent.SamplePathPosition(): I don't know what to set in the in maxDistance value, I set it to 1000 for testing purposes and it's always giving me back the info that the agent can walk to the destination but it's true.

    How can I test if an Agent will be able to walk to a destination?
     
  4. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Alright so I managed to make it work. What one should use to know if you can go towards a path is this:

    NavMesh.SamplePosition and then check if the path can be completed
    Code (CSharp):
    1. if (path.status == NavMeshPathStatus.PathComplete)