Search Unity

Finding if object can act as cover against a firing position?

Discussion in 'Scripting' started by RichBosworth, Apr 6, 2013.

  1. RichBosworth

    RichBosworth

    Joined:
    May 26, 2009
    Posts:
    325
    Hello,

    I want to implement a very basic cover system for the AI in my game. I've based this on a system of "Cover Slots", which are placed around the level, surrounding specific objects that can act as cover.

    In the situation that the player begins to fire on an enemy, I want that enemy to be able to move to the nearest cover slot that provides protection from the player's position:

    $Aim.png

    The box represents a cover object, the circles represent cover slots. The player is at the top, and the AI enemy is at the bottom.

    Currently, my code is as follows:

    Code (csharp):
    1.     /// <summary>
    2.     ///     Finds the nearest cover slot that provides cover against a certain position (e.g. where an enemy character is).
    3.     /// </summary>
    4.     /// <param name="positionToTakeCoverFrom"></param>
    5.     /// <param name="position"></param>
    6.     /// <returns></returns>
    7.     public CoverSlot FindNearestCoverSlotProvidingCoverAgainstPosition(Vector3 positionToTakeCoverFrom, Vector3 position)
    8.     {
    9.         // Find the nearest cover slots.
    10.         var coverSlots = FindSceneObjectsOfType(typeof (CoverSlot)) as CoverSlot[];
    11.         IOrderedEnumerable<CoverSlot> coverSlotsInDistanceOrder =
    12.             coverSlots.OrderBy(
    13.                 slot =>
    14.                 Vector3.Distance(new Vector3(position.x, 0, position.z),
    15.                                  new Vector3(slot.transform.position.x, 0, slot.transform.position.z)));
    16.  
    17.         // Get the direction data.
    18.         Vector3 directionBetweenPositions = (positionToTakeCoverFrom - position).normalized;
    19.  
    20.  
    21.         // Filter those in the wrong direction.
    22.         return
    23.             coverSlotsInDistanceOrder.First(
    24.                 slot =>
    25.                 (new Vector3(positionToTakeCoverFrom.x, 0, positionToTakeCoverFrom.z) -
    26.                  new Vector3(slot.FacingDirection.x, 0, slot.FacingDirection.z)).normalized == directionBetweenPositions);
    27.     }
    I can see that it would not work, due to the last part being too "specific" (correct wording?). On debugging, I'm also a little bit confused about how 3 cover slots can all have the same normalised direction vector. See below:

    $Confusion.png

    The bottom vector represents the direction between the enemy AI object and the player object (at the top of the screen, behind the cover box). The top vector is equal for all three of the cover slots (the 3 cubes next to the cover box). This confuses me, as the direction between each slot and the player object is clearly different.


    Can anybody suggest the best way to implement this?

    Thanks,
    Richard
     
  2. ardo314

    ardo314

    Joined:
    Jul 7, 2012
    Posts:
    345
    Just to clarify:
    positionToTakeCoverFrom is the players position?
    position is the ai position?

    If thats the case i dont see where you are computing the direction of the coverslots other than slot.FacingDirection but that property would be bound to be the same as they are all rotated in the same direction in your image because you dont ell them where the player is.