Expected "else" error for 'if not is_on_floor' after adding 3rd person setup

Godot Version

Godot 4.7.2 stable.steam

Question

I’m very new to coding and this is my first ever project (barebones 3D platformer). I’m having an issue while setting up controller inputs for looking around in 3rd person.

I’ve followed this video https://www.youtube.com/watch?v=D_Cz6I83TUY on setting up the 3rd person camera with a controller and ran into an error that no one else seems to have run into and I have no clue what its asking me to do. Here’s a pic with all the errors that pop up:

I didn’t have an issue with this before, but once I started adding in the extra code it’s started to break and I have no idea why. I’ve tried moving code around/fixing small mistakes but it still persists. Here’s the script to look at:


extends CharacterBody3D

@onready var camera_pivot: Node3D = %"Camera Pivot"
@onready var camera_controller: Node3D = %"Camera Controller"

const SPEED = 5.0
const JUMP_VELOCITY = 4.5

func \_physics_process(delta: float) -> void:

var axis_vector = Input.get_vector("camera_left", "camera_right", "camera_up", "camera_down")
if axis_vector.length() >=  0.2:
	camera_pivot.rotate_y(deg_to_rad(-axis_vector.x \* 1.5))
	camera_controller.rotate_x(deg_to_rad(-axis_vector.y \* 1.5))
	camera_controller.rotation.x = clamp(camera_controller.rotation.x, deg_to_rad(-80.0, deg_to_rad(30.0))
	
\# Add the gravity.
if not is_on_floor():
	velocity += get_gravity() \* delta 

\# Handle jump.
if Input.is_action_just_pressed("x_button") 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("move_left", "move_right", "move_up", "move_down")

\# New vector3 direction taking into account the user input and camera location
var direction := (camera_pivot.transform.basis \* Vector3(input_dir.x, 0, input_dir.y)).normalized()
\# Rotate character mesh to face direction of movement
if input_dir !=  Vector2(0,0):
	body_mesh.rotation_degrees.y = camera_pivot.rotation_degrees.y - rad_to_deg(indput_dir.angle()) - 90
	
	if is_on_floor():
		align_with_floor(ray_cast_3d.get_collision_normal())
		global_transform = global_transform.interpolate_with(xform, 0.3)
		
elif not is_on_floor():
	align_with_floor(Vector3.UP)
	global_transforrm = global_transform.interpolate_with(xform, 0.3)
	
if direction:
	velocity.x = direction.x \* SPEED
	velocity.z = direction.z \* SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
	velocity.z = move_toward(velocity.z, 0, SPEED)

move_and_slide()

\# Make camera controller match the position of myself
camera_pivot.position = lerp(camera_pivot.position, Vector3(position.x, position.y + 2.0, position.z), 0.15)

Any help would be appreciated! I’m worried if I keep messing with it I’ll break it even more

Sometimes the line above causes errors that appear on the next line of code. Maybe the case here.

Your deg_to_rad is incorrect, there should be one more parenthesis surrounding the -80.

camera_controller.rotation.x = clamp(camera_controller.rotation.x, deg_to_rad(-80.0), deg_to_rad(30.0))

If it helps I’d recommend splitting any function call into it’s own little variable instead of as an argument. Meaning if you see parenthesis inside of parenthesis )) it may be more legible to move that out and into it’s own line. Helps your code’s legibility, and debugging can now point at these variables if something goes wrong.

    var min_angle := deg_to_rad(-80)
    var max_angle := deg_to_rad(30)
    camera_controller.rotation.x = clamp(camera_controller.rotation.x, min_angle, max_angle)

I completely missed that ), thank you!! It cleared that error up and now my code looks cleaner, but a new issue has surfaced to do with indput_dir

I did add these extra lines from the video in case that cleared it up but didn’t seem to.


@onready var body_mesh: MeshInstance3D = %"Body Mesh"
@onready var ray_cast_3d: RayCast3D = %"RayCast3D"

that line is misspelled, the variable you do have is input_dir without the extra d indput_dir.

I would bet align_with_floor is not defined, and it doesn’t appear to be in your script anywhere; either continue following your tutorial or check if you skipped that function definition.

The third issue I would guess xform is not defined, similarly check your tutorial, I don’t know what you would want to be interpolating, maybe align_with_floor is supposed to assign it?

Went back and added the extra code to do with the xform stuff from the tutorial that I missed out on earlier, it made all the code errors go away thankfully but once I try and test the scene it shows these errors and wont let me run the game.

Godot cannot find those nodes, are you sure they are named the same under this “Punch” scene? Are they marked as Unique Names properly, with the % sign by the node’s name?