Game keeps crashing, no clue why

Godot Version

Godot Version 4.2.1

Question

Messing around with 3d player movement and the SpringArm with a Camera as a newbie. All of the code seems right to me but, it crashes the moment I try and play the project.

Code for player

extends CharacterBody3D

@export var speed := 7.0
@export var running_speed := 15.0
@export var gravity := -20.0
@export var jump_power := 15.0
@export var h_accel := 35.0
@export var v_accel := 35.0
@export var max_jumps := 2
@export var jump_delay := 0.2
@export var water_multiplier := 0.5
@export var timer = Timer.new()

var movement_vector := Vector3.ZERO
var jump_amount := max_jumps
var jump_timer := Timer.new()
var in_water := false

var _velocity := Vector3.ZERO


@onready var _spring_arm: SpringArm3D = $SpringArm3D
@onready var _model: MeshInstance3D = $MeshInstance3D

func _physics_process(delta: float) -> void:
	var move_direction := Vector3.ZERO
	var modifier := 1.0

	move_direction.x = Input.get_axis("down", "up")
	move_direction.z = Input.get_axis("left", "right")
	move_direction = move_direction.limit_length(1.0)
	move_direction *= running_speed if Input.is_action_pressed("run") else speed
	move_direction.y = movement_vector.y
	movement_vector = movement_vector.move_toward(move_direction, h_accel * delta)
	move_direction = move_direction.rotated(Vector3.UP, _spring_arm.rotation.y).normalized()
	
	_velocity.x = move_direction.x * speed
	_velocity.z = move_direction.z * speed
	_velocity.y -= gravity * delta
	
	
	if _velocity.length() > 0.2:
		var look_direction = Vector2(_velocity.z, _velocity.x,)
		_model.rotation.y = look_direction.angle()
	
		# Vertical movement
	movement_vector.y = move_toward(movement_vector.y, gravity, v_accel * delta)
	if is_on_floor():
		jump_amount = max_jumps
		jump_timer.stop()
	elif jump_timer.is_stopped() and jump_amount == max_jumps:
		jump_timer.start(jump_delay)
	if Input.is_action_just_pressed("jump") and jump_amount > 0:
		movement_vector.y = jump_power
		jump_amount -= 1
		jump_timer.stop()
		
	# camerMove
	move_and_slide()
	movement_vector /= modifier
	



func _ready():
	add_child(jump_timer)
	jump_timer.one_shot = true
	timer.timeout.connect(_on_Timer_timeout)

func _on_Timer_timeout():
	jump_amount -= 1

Code for SpringArm

@onready var _spring_arm: SpringArm3D = $SpringArm3D
@export var mouse_sensitivity := 0.05

func _process(_delta: float) -> void:
	_spring_arm.translation = Translation
	
func _ready() -> void:
	set_as_top_level(true)
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventMouseMotion: 
		rotation_degrees.x  -= event.relative.y * mouse_sensitivity
		rotation_degrees.x = clamp(rotation_degrees.x, -90.0, 30.0)
		
		rotation_degrees.y -= event.relative.x * mouse_sensitivity
		rotation_degrees.y = wrapf(rotation_degrees.y, 0.0, 360.0)

Don’t you get an error on the debug console? That is kind of important.

Forgot to add it but, it says
Invalid set index ‘translation’ (on base: ‘null instance’) with value of type ‘Translation’.

But I can’t figure out how to fix it.

First off, this may run before the children nodes are ready. Guard it with if _spring_arm.is_node_ready(): or _spring_arm may be null.
Second, if Translation is a type, that assignment might not be what you intended.

1 Like

Thank you!

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