Tweening Camera FOV when sprinting

Godot Version

Godot 4.3 Web editor

Question

How would I tween the camera FOV when I sprint?

@onready var head: Node3D = $Head


var current_speed = 5.0
var direction = Vector3.ZERO
var lerp_speed = 10.0
const walking_speed = 5.0
const springing_speed = 8.0
const crouching_speed = 3.0
const mouse_sense = 0.4

const JUMP_VELOCITY = 4.5



func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _input(event):
	if event is InputEventMouseMotion:
		rotate_y(deg_to_rad(-event.relative.x * mouse_sense))
		head.rotate_x(deg_to_rad(-event.relative.y * mouse_sense))
		head.rotation.x = clamp(head.rotation.x,deg_to_rad(-89), deg_to_rad(89))

func _physics_process(delta: float) -> void:
	if Input.is_action_pressed("Sprnt"):
		current_speed = springing_speed
	else:
		current_speed = walking_speed
		
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir := Input.get_vector("Left", "Right", "Forward", "Backward")
	direction = lerp(direction,(transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized(),delta*lerp_speed)
	if direction:
		velocity.x = direction.x * current_speed
		velocity.z = direction.z * current_speed
	else:
		velocity.x = move_toward(velocity.x, 0, current_speed)
		velocity.z = move_toward(velocity.z, 0, current_speed)

	move_and_slide()

Try this:

camera.fov = lerpf(camera.fov (75 if Input.is_action_pressed("sprint") else 65), delta*10.0)

Replace the “camera” with your Camera node, and 75 is sprinting fov and 65 is normal, you can adjust them.

1 Like

Line 41:Too few arguments for “lerp()” call. Expected at least 3 but received 2.
Line 5:The default value is using “$” which won’t return nodes in the scene tree before “_ready()” is called. Use the “@onready” annotation to solve this. (Warning treated as error.)

@onready var head: Node3D = $Head
@onready var standing_collision_shape: CollisionShape3D = $StandingCollisionShape
@onready var crouching_collison_shape: CollisionShape3D = $CrouchingCollisonShape
var camera = $Head/Camera3D

var current_speed = 5.0
var direction = Vector3.ZERO
var lerp_speed = 10.0
const walking_speed = 5.0
const springing_speed = 8.0
const crouching_speed = 3.0
const mouse_sense = 0.4

const JUMP_VELOCITY = 4.5
var crouching_depth = -0.5



func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _input(event):
	
	if event is InputEventMouseMotion:
		rotate_y(deg_to_rad(-event.relative.x * mouse_sense))
		head.rotate_x(deg_to_rad(-event.relative.y * mouse_sense))
		head.rotation.x = clamp(head.rotation.x,deg_to_rad(-89), deg_to_rad(89))

func _physics_process(delta: float) -> void:
	if Input.is_action_pressed("Crouch"):
		current_speed = crouching_speed
		head.position.y = lerp(head.position.y,1.8 + crouching_depth, delta*lerp_speed)
		standing_collision_shape.disabled = true
		crouching_collison_shape.disabled = false
	else:
		standing_collision_shape.disabled = false
		crouching_collison_shape.disabled = true
		head.position.y = lerp(head.position.y,1.8,delta*lerp_speed)
		if Input.is_action_pressed("Sprnt"):
			current_speed = springing_speed
			camera.fov = lerpf(camera.fov (75 if Input.is_action_pressed("sprint") else 65), delta*10.0)

		else:
			current_speed = walking_speed
		
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir := Input.get_vector("Left", "Right", "Forward", "Backward")
	direction = lerp(direction,(transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized(),delta*lerp_speed)
	if direction:
		velocity.x = direction.x * current_speed
		velocity.z = direction.z * current_speed
	else:
		velocity.x = move_toward(velocity.x, 0, current_speed)
		velocity.z = move_toward(velocity.z, 0, current_speed)

	move_and_slide()

I fixed the $head Error but now it comes up with a diffrent error
ine 41:Name “fov” called as a function but is a “float”.
Line 41:Too few arguments for “lerp()” call. Expected at least 3 but received 2.

Sorry I forgot to add a comma:

camera.fov = lerpf(camera.fov, (75 if Input.is_action_pressed("sprint") else 65), delta*10.0)

It reduces the FOV and dosnt go back?

1 Like

Are you sure? Try to print the fov. Did “sprint” input map exist?

Oh put the code line after a tab of the function not inside the sprint pressed statement. And define the sprint input map.

Yea I didn’t capitalize the S in sprint I’m going to reload and see if it fixes it

1 Like

Now it doesn’t change the FOV did I place the code in the right place?

@onready var head: Node3D = $Head
@onready var standing_collision_shape: CollisionShape3D = $StandingCollisionShape
@onready var crouching_collison_shape: CollisionShape3D = $CrouchingCollisonShape
@onready var camera: Camera3D = $Head/Camera3D

var current_speed = 5.0
var direction = Vector3.ZERO
var lerp_speed = 10.0
const walking_speed = 5.0
const springing_speed = 8.0
const crouching_speed = 3.0
const mouse_sense = 0.4

const JUMP_VELOCITY = 4.5
var crouching_depth = -0.5



func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _input(event):
	
	if event is InputEventMouseMotion:
		rotate_y(deg_to_rad(-event.relative.x * mouse_sense))
		head.rotate_x(deg_to_rad(-event.relative.y * mouse_sense))
		head.rotation.x = clamp(head.rotation.x,deg_to_rad(-89), deg_to_rad(89))

func _physics_process(delta: float) -> void:
	if Input.is_action_pressed("Crouch"):
		current_speed = crouching_speed
		head.position.y = lerp(head.position.y,1.8 + crouching_depth, delta*lerp_speed)
		standing_collision_shape.disabled = true
		crouching_collison_shape.disabled = false
	else:
		standing_collision_shape.disabled = false
		crouching_collison_shape.disabled = true
		head.position.y = lerp(head.position.y,1.8,delta*lerp_speed)
		if Input.is_action_pressed("Sprnt"):
			current_speed = springing_speed
		else:
			current_speed = walking_speed
		camera.fov = lerpf(camera.fov, (75 if Input.is_action_pressed("Sprint") else 65), delta*10.0)
		
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir := Input.get_vector("Left", "Right", "Forward", "Backward")
	direction = lerp(direction,(transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized(),delta*lerp_speed)
	if direction:
		velocity.x = direction.x * current_speed
		velocity.z = direction.z * current_speed
	else:
		velocity.x = move_toward(velocity.x, 0, current_speed)
		velocity.z = move_toward(velocity.z, 0, current_speed)

	move_and_slide()

It’s on line 43 btw

You forgot to add “i” in sprint, see that.

Rename the input map.

I just noticed that XD

1 Like

Thanks for the help! I got it working.

1 Like

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