Getting the FOV as a Variable

Godot Version




I am trying to get a player’s FOV as a variable. I see in the documentation that there is the get_fov function and do not understand how to use it; I have tried:

# Boring program stuff
@export var  SPEED = 5.0
const JUMP_VELOCITY = 4.5
@export var sensivity = 0.3
var fov = false
var lerp_speed= 1
var player_fov = $Camera.fov

and the player_fov line throws, as a result:

E 0:00:00:0561 @ @implicit_new(): Node not found: "Camera" (relative to "PickleRick").
  <C++ Error>    Method/function failed. Returning: nullptr
  <C++ Source>   scene/main/node.cpp:1638 @ get_node()
  <Stack Trace> @ @implicit_new()

and I would like to know an effective approach to getting the player fov as a variable, and then adjusting it. It’s mostly because I want the FOV to change when the player sprints.

Your error is Node not found: "Camera" (relative to "PickleRick").
It means it couldn’t find your camera because it was searching a node exactly named “Camera” among your “PickleRick” node’s children.

Either your camera is among the children and might not be initialized yet, so you need the @onready keyword which tells the script to wait until your children are ready to initialize the variable.

@onready var player_fov = $Camera.fov

Or your camera is higher up in the scene tree.

  • You know the camera node path and it won’t change
var player_fov = get_node("../path/to/camera/").fov
  • or you can dynamically retrieve the active camera and get the fov, but it has to be in a function
var player_fov
func _ready():
    player_fov = get_viewport().get_camera_3d().fov

Yet, I haven’t tested but I’m afraid, retrieving fov this way is just getting the value and not an actual reference to the camera properties so if you want to dynamically set it in gameplay you will need:

  • a function
func set_player_fov(new_fov):
        get_viewport().get_camera_3d().fov = new_fov

# Setting value example
#func _input(event):
#if event.is_action_just_pressed("sprint"):
#    set_player_fov = 60.0
  • or a setter (but you need write a class)
class_name Player

var player_fov : float :
        get_viewport().get_camera_3d().fov = new_fov

# Setting value example
#func _input(event):
#if event.is_action_just_pressed("sprint"):
#    player_fov = 60.0
1 Like

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