Search Unity

Enemy AI: Using Geometry To Hit Player Behind A Wall

Discussion in 'Scripting' started by Evonoucono, Apr 21, 2018.

  1. Evonoucono

    Evonoucono

    Joined:
    Feb 7, 2017
    Posts:
    7
    Hello,
    I have an enemy who can shoot projectiles which all bounce of walls. It's easy to make the enemy shoot the player directly when they can see each other. But when they can't see each other how would I make the enemy shoot it's projectiles so it would bounce off a wall to hit the player? (with precision)
    I attached an image to help explain what I mean. The only solution I was able to think of was to send a bunch of Raycasts out from the enemy, each of which would hit a wall, then check if that point on the wall can see the player, but that seemed inefficient and not very precise when the enemy is far away from the player.
    What would be the correct way of doing this?

    Thanks.

    download.png
     
    Last edited: Apr 21, 2018
  2. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    If your using a pathfinder you can use that to help with this, shoot a ray down the way your path is, if it hits a wall get the walls position. Do the triangle math required to work out where the enemy would need to shoot that wall to hit the player, fire a raycast at the wall, if it doesnt hit, you cant see that part of the wall. If it does hit do another raycast at the way your projectile would leave the wall, if it hits the player fire, if not dont.

    So thats the general gist of it.

    For shooting down the path, follow the points on your path as far as you can in the same pattern. E.g. If they the next 6 points are diagnol in the same direction the next goes right aim at the last diagnol point. If its left,up,left,up,left,left; aim at before the second left. A pattern in your path will form a clear line for you to find a wall to shoot at :) If a pattern doesnt exist look at the first point.

    Hope this helps in someway.
     
    Evonoucono likes this.
  3. Evonoucono

    Evonoucono

    Joined:
    Feb 7, 2017
    Posts:
    7
    I'll give it a try, I think I understand what you mean. Thanks