← Pixel Dreamer Studio

A first-person gun that walls cannot cut

Draw the gun with a second camera in a SubViewport on top of the game, and put your lights on the gun's layer too.

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.

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

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