3d camera with limited view

I want a game something similar to fallout shelter. For this purpose I have created a vertical StaticBody3D and opposite to it a camera which should move and see something only in the area of StaticBody3D without going beyond the boundaries.

Moving and some zooming of the camera I managed to create.

class_name FreeLookCamera extends Camera3D


var camera = null

var zoom_speed = 1.0  

@export_range(0.0, 1.0) var sensitivity = 0.1


func  _ready():
   camera = get_node("/root/Node3D/Camera3D")


func _input(event):
# Receives mouse motion
if event is InputEventMouseMotion:
	if event.button_mask == MOUSE_BUTTON_RIGHT:
		var sensitivity = 0.005
		var movement = Vector3(-event.relative.x, event.relative.y, 0) * sensitivity
		var target_position = position + movement

		var plane_position = get_node("/root/Node3D/StaticBody3D").global_transform.origin
		var min_x = plane_position.x - 1
		var max_x = plane_position.x + 1
		var min_y = plane_position.y - 1
		var max_y = plane_position.y + 1
		
		var view_direction = -global_transform.basis.z.normalized()
		position = Vector3(
			clamp(target_position.x, min_x, max_x),
			clamp(target_position.y, min_y, max_y),
			target_position.z
		)
		translate(view_direction * movement.z)

if event is InputEventMouseButton:
	if event.button_index == MOUSE_BUTTON_WHEEL_UP:
		translate(global_transform.basis.z * -zoom_speed) 
	elif event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
		translate(global_transform.basis.z * zoom_speed)

However, a way to limit the camera’s movement and visibility I still couldn’t find. Can anyone help with this?

You are already clamping the position, what do you mean?

For clarity, I’ve added a texture
If I move the camera too far away I can see the area outside of StaticBody3D.
Okay, that’s fixed by limiting the ability to distance yourself.

But when I zoom in on the camera, the limits beyond which the camera cannot move seem to have shrunk. I brought the camera closer and now I can’t see the part by the square F1 and F8


If I bring the camera even closer, my camera travel area is further reduced to a 4 by 4 square area.
I can’t fix this moment. Yeah and I was wondering if anyone had done something like this before me.

I’m not sure if I understand what you’re saying correctly, or if I’m interpreting your code correctly, so excuse me if I say something incorrect, but it seems to me all you need to do is multiply your limits by the inverse of the zoom, so:

becomes

		position = Vector3(
			clamp(target_position.x, min_x / target_position.z, max_x / target_position.z),
			clamp(target_position.y, min_y / target_position.z, max_y / target_position.z),
			target_position.z
		)

You may need to adjust it by a factor based on zoom_speed, I am not sure. If you post your project files it will be much easier to give you an answer, because some of this depends on how your viewport is set up.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.