How to Change Fov of 3D camera, it only works with expressions?

Godot Version

Godot Version 4.5.1

Question

Hello, I am having an issue with my code where I am trying to set the 3D Cameras Fov in the player script. I was able to get the Fov change to work when writing the script like an expression:

if sprint_toggle == true:
	_camera.fov -= 30
			
else sprint_toggle == false:
	_camera.fov += 30

but when I tried writing it as a set value it would only set it once and never update:

if sprint_toggle == true:
	_camera.fov = 100
			
else sprint_toggle == false:
	_camera.fov = 70

is there some kind of syntax to use to set the Fov or is it just preferred to use expressions to add/subtract from it?

Hi Edwin_Frog,

the behavior you are describing is to be expected. Your first code snippets adds and subtracts repeatedly. Your second code snippet updates to the same value every time – and therefore there is change once and never again.

Note that the expression _camera.fov -= 30 means the same as _camera.fov = _camera.fov - 30 (it is a short form for exactly this!), which means the current field-of-view (Fov) value is read, then modified (-30) and finally written back into the Fov property (Unlike in Math in programming the expression a = b is not symmetrically but assigns b into a, which means it moves what is on the right of the equation into what is on the left – that is also why there can only be variable names on the left side).

Kind regards :four_leaf_clover:

P.S.: I am now going through the documentation for the Fov of a Camera3D. It says there the Fov property represents the angle in degrees. So this field is not just a regular float. That is something to keep in mind.

1 Like