When the player walks up to a wall, the end of a first-person gun or the hands disappear into it, because the gun is an ordinary 3D object a little in front of the camera. In our Godot 4.7 test, a wall 35 cm in front of the camera hid 1,316 of the gun's 16,703 pixels. The common quick fix, turning off the depth test on the gun's material, showed the gun but drew its parts in the wrong order: only 15,818 of its pixels matched the picture without the wall. The script below draws the gun with a second camera in its own SubViewport, on top of the game. With the wall right there, all 16,703 pixels matched.
The code
Save it as weapon_view.gd, add a Node anywhere in your scene with this script, and set main_camera to the player's camera and gun to the node that holds the gun meshes. The gun stays where it is, usually a child of the camera. The script only changes which camera draws it.
extends Node
## Draws your first-person gun or hands in their own SubViewport on top of the game,
## so a wall right in front of the player never cuts into them.
## Add it anywhere in the scene, then set main_camera and gun in the Inspector.
@export var main_camera: Camera3D
@export var gun: Node3D # the node that holds the gun meshes, usually a child of the camera
@export_range(1, 20) var layer := 20 # a render layer that nothing else in the game uses
@export var gun_fov := 0.0 # 0 = the same as main_camera; lower = a bigger gun
var _view: SubViewport
var _cam: Camera3D
func _ready() -> void:
var bit := 1 << (layer - 1)
for n in [gun] + gun.find_children("*", "VisualInstance3D", true, false):
if n is VisualInstance3D:
n.layers = bit
main_camera.cull_mask &= ~bit # the game camera no longer draws the gun
for light in get_tree().root.find_children("*", "Light3D", true, false):
light.layers |= bit # a camera only uses the lights on the layers it draws
_view = SubViewport.new()
_view.transparent_bg = true
_view.render_target_update_mode = SubViewport.UPDATE_ALWAYS # it is not inside a SubViewportContainer
_view.size = get_viewport().get_visible_rect().size
_cam = Camera3D.new()
_cam.cull_mask = bit # this camera draws only the gun
_cam.fov = gun_fov if gun_fov > 0.0 else main_camera.fov
_cam.near = 0.01
_view.add_child(_cam)
add_child(_view)
var on_top := CanvasLayer.new()
var rect := TextureRect.new()
rect.texture = _view.get_texture()
rect.set_anchors_preset(Control.PRESET_FULL_RECT)
rect.mouse_filter = Control.MOUSE_FILTER_IGNORE
on_top.add_child(rect)
add_child(on_top)
get_viewport().size_changed.connect(func(): _view.size = get_viewport().get_visible_rect().size)
process_priority = 1000 # after the scripts that move the camera
func _process(_delta: float) -> void:
_cam.global_transform = main_camera.global_transform
if gun_fov <= 0.0:
_cam.fov = main_camera.fov # follows a zoom
With the player moved 50 cm to the side, the gun stayed in the same place on screen: 16,703 pixels again.
What we measured
A gun made of two boxes, a barrel and a grip, as a child of the camera, one DirectionalLight3D, and a dark wall whose front was 35 cm in front of the camera. We compared each picture with the one taken without the wall. Two runs gave the same numbers.
- No wall: 16,703 red gun pixels.
- With the wall: 15,387. The front of the barrel was inside the wall.
no_depth_teston the gun's material: 18,255 red pixels, only 15,818 of the gun's pixels the same as without the wall, and 1,552 red where that picture had dark sides of the gun.- The script: 16,703, every one the same as without the wall, none extra. The main camera no longer drew layer 20.
- The script with the light left on layer 1 only: 0 red pixels. The barrel came out 0.02 red, almost black, against 0.89 with the light on layer 20 as well.
gun_fovat 60 with the camera at 75: 20,626 pixels, a bigger gun on screen.
Why it happens
The gun is drawn like everything else, and the depth buffer keeps whatever is closest to the camera, so a wall nearer than the end of the barrel covers it. Turning off the depth test makes the gun ignore the wall, but also its own depth: its parts are drawn in whatever order they come, and a part at the back can cover one at the front.
The SubViewport has a depth buffer of its own with only the gun in it, so the gun is sorted against itself and never against the wall. A camera also leaves out the lights that are not on the layers it draws, which is why the gun went black until the light was on layer 20 too.
What to change
layer: a render layer nothing else uses. The script puts the gun's meshes on it and takes it off the main camera.- Lights: when it starts, the script adds every Light3D in the scene to that layer. A light added later, such as a muzzle flash, needs
light.layers |= 1 << 19for layer 20, or it will not light the gun. gun_fov: 0 uses the main camera's and follows a zoom. A lower value draws the gun bigger.
Where the shot goes: a shot that lands on the crosshair. The flash at the barrel: a muzzle flash with a light. Recoil that does not undo the aim: recoil kept apart from the aim. The marks the bullets leave: bullet holes in one draw call.
The shooting itself: FPS Combat Kit for Godot 4 has hitscan shooting, impacts that change with the surface they hit, 7 weapon effect presets, a shooting range demo and 39 gun effects.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: FPS Combat Kit for Godot 4 has hitscan shooting, impacts that change with the surface they hit, 7 weapon effect presets, a shooting range demo and 39 gun effects.
More how-tos for FPS Combat Kit for Godot 4: Muzzle flash with a real light · Flash a 3D enemy white when it takes a hit
More how-tos: Projectiles and aiming
- Bullet holes and blood splats in one draw call
- A ball that hits the net however hard it is kicked
- A shot that meets a moving target
- An Area3D that does not see a bullet or another area
- A fire rate that holds at any frame rate
- Point swords, projectiles and models that face +Z at the target
- FPS shots that land on the crosshair
- A spell that keeps flying straight after it leaves the hand
- Turn towards a target the short way round
- A laser beam that stops where it hits, the moment you aim
- Fast projectiles that never fly through enemies
- A trajectory line that matches where the ball lands
- Aim a spell at the mouse on a 3D floor
- A homing missile that turns, but not instantly
- Particles fall sideways when you rotate the emitter
- Particles that fly into the player
- Gaps in the trail of a fast emitter
- Make a tower shoot the enemy closest to your base
- Hit the target with an arcing shot at any angle and height
- Kick a RigidBody3D ball with the same strength at any tick rate
- Reload without losing or making up rounds
- Fan out any number of projectiles evenly around the aim direction
- Recoil that settles back to where the player aims