Boxcast Not Detecting Stuff

I am implementing an “attack” state for my little characters to, of course, MURDER one another. It is currently pretty fine (just needs some refactoring later), but there’s one problem.

The characters are programmed to run around and shoot each other. When the characters get too close, they will stop (but still keep shooting). The first behavior (running around shooting like a madman) works fine. However the second behavior (stopping to shoot) works fine except for one thing: the Box Casts used for shooting do not detect anything.

My game’s shooting utilizes BoxCasts.

Shooting Script, in a terribly named “dudeScript”:

public void Shoot()
    {
        if (Time.time > nextFiringTime)  // shooting timing
        {
            if (currentWep.FireRate != null) nextFiringTime = Time.time + currentWep.FireRate; 
            if (currentWep.FireSound != null) objSFX.PlayOneShot (currentWep.FireSound, 5f);
            RaycastHit objHit; 
            print ("time " + Physics.BoxCast (center: gunShooter.transform.position, halfExtents: currentWep.WepExtents, direction: gunShooter.transform.forward, hitInfo: out objHit, orientation: Quaternion.identity));
            if (Physics.BoxCast (center: gunShooter.transform.position, halfExtents: currentWep.WepExtents, direction: gunShooter.transform.forward, hitInfo: out objHit, orientation: Quaternion.identity)) //These values derived from a Weapon class which I can customize.
            {
                Vector3 directionToTarget = (objHit.transform.position - this.transform.position).normalized;
                float distanceToTarget = Vector3.Distance (this.transform.position, objHit.transform.position);

                RaycastHit obstacle;


                if (!Physics.Raycast (origin: gunShooter.transform.position, direction: directionToTarget, maxDistance: distanceToTarget, layerMask: 9, hitInfo: out obstacle))
                {
                    dudeScript dude = objHit.collider.GetComponent<dudeScript> ();
                    ///OMITTED CODE
                    entityGangManager seenGangManager = objHit.collider.GetComponent<entityGangManager> ();


                    if (dude && seenGangManager.seen.Contains (this.gameObject) == false)
                    {
                        dude.health -= currentWep.Damage;
                    }

                    //OMITTED CODE

                    if (ai) ///Finds an AI script and tells the AI script to get MAD and attack!
                    {
                        ai.state = "attacking";
                    }

                }   

            }

        }

    }

Code explanation:

The character casts a Box Cast from the “gunShooter” point (which is where the the bullet is fired from). A Ray Cast checks for any obstacles. The script doesn’t go as far as the BoxCast portion in the aforementioned stop-then-shoot behavior, saying that the BoxCast hasn’t detected anything. In comparison, the chase & shoot behavior works fine.

Now we have another piece of code here, detailing the Attack() function which is called when the characters spot an aggressive enemy.

    void Attack(List<GameObject> victims)
    {
        if (victims.Count > 0)
        {
            GameObject closestVictim = FindClosestGameObject (victims);
            print (Vector3.Distance (this.transform.position, closestVictim.transform.position) < 10);
            if (Vector3.Distance(this.transform.position, closestVictim.transform.position) < 10) //stop & shoot, activated when too close to enemy (distance for this decision is subject to change)
            {
                agent.SetDestination (FindClosestGameObject (victims).transform.position);
                agent.isStopped = true;
                this.transform.LookAt (target: closestVictim.transform, worldUp: Vector3.zero);
                dude.Shoot (); //calls the shoot method which is contained within a script also in the same GameObject
            }
            else //chase & shoot
            {
                agent.isStopped = false;
                agent.SetDestination (FindClosestGameObject (victims).transform.position); 
                dude.Shoot ();
            }

        }
        else
        {
            state = "wander";
        }

    }

It finds the closest person and starts to shoot at them. If they are too close, it stops & shoots. Otherwise the enemy will chase & shoot. If there are no enemies, then it will wander.

Even when I change the extents to 1000 on x, y and z, it still won’t detect with the chase & shoot movement.

Here’s what I tried that did not work:

  • a bool method that detects whether or not the enemy can be shot

  • changing 10 in (Vector3.Distance(this.transform.position, closestVictim.transform.position) < 10)

  • increasing box cast size (mentioned beforehand)

I do not believe the Weapon class causes this, because as mentioned the shooting works normally in the chase & shoot behavior.

Any suggestions?

I’ve always had issues with Boxcast, now I know that is my own fault for not understanding exactly how it works, but I always tend to go for the Spherecast or Capsulecast which I personally find to be more useable.

Someone here might be able to explain Boxcast better, but just in case, you could try the other two.

Update: the Raycast used to check for obstacles in the Shoot() function is supposed to check for Game Objects in Layer 9 (the obstacle layer) but it detects other layers as well. This disables NPCs from shooting, but sometimes they can shoot. NPCs are essentially confusing other NPCs for obstacles because they see them as a part of the obstacle layer mask.