How to stop gun from clipping through walls

Godot Version

Godot 4.3

Question

` In my current project, I’m trying to make it so the players gun doesn’t clip through the wall when they get to close, as seen here:


I’ve seen a few methods to do this, but both seem to have problems. The first method involves using sub viewports, as seen in this video,

However, this ends up causing the guns to not react to the shadows from the world, which apparently wasn’t a problem in Godot 3, but now is in Godot 4.

The next method is using a shader, as seen here,

However, this method does technically “work”, it ends up changing the size and position of the models, and also causes the gun and the hand to not render correctly, as seen in these pictures:



I’m wondering what I have to do to overcome these issues and implement either of these methods. Any help is greatly appreciated.
Thanks.

`

Maybe use a spring arm? Attach a collider to a spring arm (one that covers the entire gun) and attach the gun to the collider. This might not work and might look funny if it does, but maybe it’s worth a try?

Add a StaticBody3D and a CollisionShape3D to the gun so it collides with the wall.

Unfortunately giving the arm a CollisionShape would cause issues having the player fit into certain areas.

If you know what those areas are, put an Area3D over them, and on entry disable the collision shape on the gun, and on exit, re-enable it. Alternately, if it’s like squeezing through a crack or something, lower the gun in those areas.