Prevent shooting when gun is inside wall

Hi

The Guns in my game do not have a collider on them.
They have CapsuleCollider with isTrigger = true. This is on purpose because of the desired character movement I want to achieve on the character (only the player’s capsule collider is used to prevent going through walls)
So, as a result the gun can enter the walls and shoot through.
To prevent shooting when the gun’s inside a wall, I did this :
The gun has a script : GunCollissionHandler
Since the gun has a CapsuleCollider Trigger, I do this:
Whenever gun enters a wall,

OnTriggerEnter(Collider other){

       if(other.layer = "Wall"){

              isInsideWall = true;
       }
}

OnTriggerExit(Collider other){

       if(other.layer = "Wall"){

              isInsideWall = false;
       }
}

Then when shoot button is pressed, obviously I shoot when isInsideWall is false.

But this has a problem (at the corners of the room apparently). When character rotates, the gun moves from inside one wall to another. It’s isInsideWall becomes wall and It can shoot, even though it is inside another wall.
I even tried counting the colliders, like this :

OnTriggerEnter(Collider other){
 
        if(other.layer = "Wall"){
 
               numWallsInside++;
        }
 }
 
 OnTriggerExit(Collider other){
 
        if(other.layer = "Wall"){
 
               numWallsInside--;
        }
 }

Then, when shooting, check if (numWallsInside < 1). This also fails in some cases of death and respawn.
I hope you understand the problem.

Thanks.

Okay, I’m kinda new to this but here’s what I’m thinking.

First of all, most games make the player’s collider large enough that the gun won’t go into the wall in the first place. Will this not work with your game?

Second, if you’re sure that your guns can pass through walls, then don’t try to constantly track whether your gun is currently inside a wall. Too difficult, right? Rather, quick caste a ray from your head to your gun. If the ray hits a wall before it hits your gun, then you know your gun is in a wall.

Just a though… I haven’t tested it myself.