Preventing Visual Object PassThrough

Or “My Gun is in the Wall, Now what?”

Thomas Kesler
3 min readOct 30, 2022

Objective: Prevent the Visual Glitch of objects (such as Guns) passing through other objects (such as Walls) when too close.

When making games, putting colliders on everything and expecting the physics engine to figure it all out can be terribly expensive in processing power and costly in the Frame Rate. This is why we Fewer colliders with fairly primitive shapes in games, even today. But this leaves us with the occasional unsatisfying experience of watching something pass through a wall or box that shouldn’t. We can use a fairly simple, once you know it, trick to prevent this from happening.

First thing we want to do is to setup a new layer for our game objects. Here I have called it the Weapon layer because we will be using to prevent our player’s weapons from phasing through walls and objects. When you first switch layers, make sure to apply the layers to the children objects as well.

Next, we will need an additional camera. Create a new Camera as a child of the Main Camera or the one attached to the player. Set the Render Type to Overlay instead of base.

In the Rendering section of the new camera, go to Culling Mask and make sure just your Weapon Layer is selected. This means that this camera now only sees/shows what is on the Weapon layer.

Now over in the Main Camera, find the Culling Mask under the Rendering section and uncheck Weapon from this list. This Camera will no long see the Weapon layer.

The Final step is to find Stack under Rendering in the Main Camera. Opening you can add Overlay Cameras to the main Camera. This cause the Weapon Camera to display “on top” of the Main camera and give the effect of the Weapon not intersecting the World objects.

With this tool now at hand, I wonder what other tricks we can use for our games.

--

--

Thomas Kesler

A Unity Developer with a fondness for Fantasy games and the challenge of pushing boundaries.