Solving Weapon Clipping in Unity

If you, like me – have started writing an FPS game and have noticed that when you walk face first into an object whilst proudly pointing your gun at it, half your weapon is swallowed. Kind of like the image below:

Weapon clipping into wall

So I thought I’d look to the Unity team to understand how they have solved this problem in one of their demo projects, thus, my solution is based loosely on the FPSMicrogame.

If you would prefer to see a video explanation – you can check out this YouTube video I made, if not, read on beautiful game developer.

In the hierarchy under the main camera object we’ll add a second camera whose purpose in life will be to render only the weapon and nothing else, we’ll call it the weapon camera.

Second camera

We are going to need to tell the main camera that it should ignore weapons, but render everything else. We can do this by adding layers, we’ll ad a layer called Weapons:

Add Layer

Once our new layer has been added, we can assign it to our weapon – in my case that is the desert eagle, to do this, click select the object you want to apply the layer to and select the newly created layer in the layers dropdown list:

Assign weapon layer to weapon

You will be presented with a popup asking you if you would like to apply the layer to all child objects, you can say yes to this as we want our camera to ignore all parts of the weapon:

Apply layer to children

Next, with the main camera game object selected – click the culling mask dropdown list and remove the weapon layer:

Deselect weapon from culling mask

With the weapon camera game object selected, remove everything except for the weapons layer:

Weapons layer on weapon camera object

One last thing to note – if you’re working in the universal render pipeline you will need to turn HDR and MSAA off on the weapon camera – as it seems these effects stack causing some weird behaviour:

HDR & MSAA

And that’s that, you should no longer have any visible weapon clipping. I say visible, as the weapon will still clip through any objects if you’re looking at it from the scene view, but as far as the player is concerned their weapon is not being swallowed by a wall which is nice.

Anyway – I would really appreciate it if you subscribed to my YouTube channel or shared this article with anyone who might find it useful.

Leave a Comment

Your email address will not be published. Required fields are marked *