Limit the player's field of view to a dynamic interval based on -transform.basis.z

Godot Version

4.6

Question

Hi,

In my game (3D), I would like to prevent the player from being able to look completely behind them.
The idea would be to limit their field of view to a dynamic angular range based on their forward direction (-transform.basis.z).

Specifically:
I would like to constrain the rotation of the gaze to a range such as:
[180°, -180°]
But relative to the player’s current direction (so not in fixed world space, but rather in local space based on -transform.basis.z).

Basically:
The player’s forward direction defines the center of the field of view
Only a certain arc around this vector is allowed
It is impossible to turn the camera beyond this (so no looking completely behind)

If you have already implemented this kind of constraint properly in Godot (3D), I’m interested.
Thanks!

-180,180 range is full 360 degrees

2 Likes

If the camera is parented to the body then it should be very simple, you just have to clamp the camera’s rotation to the allowed angle. Something like this in the camera’s script:

func _process(delta: float) → void:
    rotation.y = clampf(rotation.y, -allowed_angle, allowed_angle)

Assuming that the camera’s rotation.y is 0 when looking forward.