Shooting Units at Corners

I have a cover system set up that works for the most part; however, I want units able to shoot other units that are at wall corners. Right now, the raycast can be stopped by the obstacle giving full cover, which makes it impossible to target that unit.

I tried altering the code in ShootAction.cs by including a check shown below

if (Physics.Raycast(
                    unitWorldPosition + Vector3.up * unitShoulderHeight,
                    shootDir,
                    Vector3.Distance(unitWorldPosition, targetUnit.GetWorldPosition()),
                    obstaclesLayerMask))
                {
                    if (targetUnit.GetCoverType() == CoverType.None || targetUnit.GetCoverType() == CoverType.Half)
                    {
                        //Blocked by obstacle and no full cover
                        continue;
                    }
                }

However, this has the problem of letting units shoot through walls if the target has full cover. Is there a way to stop a ray once it touches an obstacle before it gets to the target unit? Or is there some other way of allowing a unit to shoot at a target with full cover only if there are no other obstacles between them?

The description I’m inferring from the code is that your CoverType is

public enum CoverType
{
    None,
    Half,
    Full
}

The if statement indicates that if the covertype is none or 1/2, then you should continue and do nothing, as indicated by the comment within the method //Blocked by obstacle and no full cover

In this case, the if check should probably be

if(targetUnit.GetCoverType() == CoverType.Full)
{
    continue;
}

to satisfy that condition.

Unfortunately that doesn’t solve the problem because now any unit can be shot through walls except those with full cover.

What I was trying to do was have it where for a square of cover that gives full cover but also blocks the raycast, such as a tree, the shooter can still shoot the target. With the original code, the raycast would be blocked and so the unit couldn’t be targeted. With the code I put in the original post, the unit could be targeted, but could also be targeted through walls.

So, I need some way for the raycast to register when it hits only one obstacle, like a tree, and if it hits two, like a wall and a tree, then it doesn’t allow the unit to be targeted.

This is one I never found a perfect answer to… While it doesn’t seem like a lot, you’ve described some tricky corner cases…

  • No cover, no wall Clear Shot
  • No cover, wall behind me Clear shot
  • No cover, wall in front of me No shot
  • Full cover No Shot
  • Half cover, no wall or wall behind me Shot
  • Half cover, wall in front of me No Shot

I honestly haven’t done much with cover, so I’m unsure if I’m going to do more harm than good at this point…

I’m not sure of the critera for full cover or 1/2 cover… and how that is stored… and what you mean by a wall… to me… if the bullet hits the wall, it’s done… same with a tree…

I guess I need to see more of your cover code to even know if I’m on the right track in my thinking.

Privacy & Terms