Godot Version
Godot 4.6
Question
I’m a bit new to coding, so this might be a stupid question but…
whenever I try to run the application, I get the error message
E 0:00:00:451 _physics_process: Invalid operands ‘float’ and ‘Nil’ in operator ‘*’.
player.gd:55 @ _physics_process()
player.gd:55 @ _physics_process()
even though there’s no red lines in my code, it seems to be at line 55, but the rest is baffling.
This is my code:
extends CharacterBody3D
## The movement speed in m/s. Default is 5.
#@export_range(1.0,30.0) var speed : float = 5.0
@export_range(1.0,30.0) var speed : float = 5
## The Jump Velocity in m/s- default to 6.0
@export_range(2.0,10.0) var jump_velocity : float = 6.0
## Mouse sensitivity for looking around. Default is 3.0
@export_range(1.0,5.0) var mouse_sensitivity = 3.0
var mouse_motion : Vector2 = Vector2.ZERO
var pitch = 0
## The amount of acceleration on the ground- less feels floaty, more is snappy-[br]Default is 4
@export_range(1.0,10.0) var ground_acceleration := 4.0
## the amount of acceleration when in the air. less feels more floaty more is more snappy.[br]Default is 0.5
@export_range(0.0,5.0) var air_acceleration := 0.5
# Get the gravity from the project settings to be synced with RigidBody nodes.
@export_range(5.0,25.0) var gravity : float = 15.0
# the camera pivot for head pitch movement
@onready var camera_pivot : Node3D = $CameraPivot
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
# velocity.y -= gravity * delta
velocity.y -= gravity * delta
# Handle Jump.
# if Input.is_action_just_pressed("ui_accept") and is_on_floor():
# velocity.y = jump_velocity
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("ui_left", "ui_right", "ui_up", "ui_down")
var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y))
var target_velocity := Vector3.ZERO
if direction:
target_velocity = direction
#now apply velocity with lerp based on whether on ground or in air
if is_on_floor():
velocity.x = move_toward(velocity.x , target_velocity.x * speed , speed * ground_acceleration * delta)
velocity.z = move_toward(velocity.z, target_velocity.z * speed, speed * ground_acceleration * delta)
else:
velocity.x = move_toward(velocity.x , target_velocity.x * speed , speed * air_acceleration * delta)
velocity.z = move_toward(velocity.z, target_velocity.z * speed, speed * air_acceleration * delta)
#now actually move based on velocity
move_and_slide()
#rotate the player and camera pivot based on mouse movement
rotate_y(-mouse_motion.x * mouse_sensitivity / 1000)
pitch -= mouse_motion.y * mouse_sensitivity / 1000
pitch = clampf(pitch,-1.35,1.35)
camera_pivot.rotation.x = pitch
#reset it
mouse_motion = Vector2.ZERO
#haif le and store mouse motion
func _input(event: InputEvent):
if event is InputEventMouseMotion:
mouse_motion = event.relative
#if you click the flag to the next level
var d_flag = global_position.distance_to(%flag.global_position)
if Input.is_action_just_pressed("ui_left_click") and d_flag <= 5:
get_tree().change_scene("lvl_1.tscn")