I'm relatively new to Godot 4.0.3. I have two problems which seem to depend on each other so I put both of them into one single question.
I know, these issues have been reported multiple times on other forums, but none of their solutions worked for me and when I try to fix one of them the other one occurs again or gives errors.
My first problem (_ready(): Node not found: "Pivot/Camera"):
my full script:
extends CharacterBody3D @onready var camera = $Pivot/Camera var gravity = -30 var max_speed = 8 var mouse_sensitivity = 0.002 # radians/pixel #var velocity = Vector3() func get_input(): var input_dir = Vector3() # desired move in camera direction if Input.is_action_pressed("ui_up"): input_dir += -global_transform.basis.z if Input.is_action_pressed("ui_down"): input_dir += global_transform.basis.z if Input.is_action_pressed("ui_left"): input_dir += -global_transform.basis.x if Input.is_action_pressed("ui_right"): input_dir += global_transform.basis.x input_dir = input_dir.normalized() return input_dir func _unhandled_input(event): if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED: rotate_y(-event.relative.x * mouse_sensitivity) $Pivot.rotate_x(-event.relative.y * mouse_sensitivity) $Pivot.rotation.x = clamp($Pivot.rotation.x, -1.2, 1.2) func _physics_process(delta): velocity.y += gravity * delta var desired_velocity = get_input() * max_speed velocity.x = desired_velocity.x velocity.z = desired_velocity.z move_and_slide() Error:
E 0:00:00:0961 main.gd:3 @ _ready(): Node not found: "Pivot/Camera" (relative to "/root/Root/Player"). <C++ Error> Method/function failed. Returning: nullptr <C++ Source> scene/main/node.cpp:1364 @ get_node() <Stack Trace> main.gd:3 @ _ready() (I got the code from https://kidscancode.org/godot_recipes/3.x/g101/3d/101_3d_07/index.html and edited it to hopefully fix some errors)
What I've tried:
Removing the @ on @onready (I've seen onready without the @ on other forums too):
Error at (3, 1): Unexpected "Identifier" in class body. Removing the entire @onready:
Parser Error: 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.) My second problem (player falls through floor):
I have a MeshInstance (the floor mesh) inside a CollisionShape3D, and also the player has two CollisionShape3Ds, but none of the colliders actually work.
I tried changing the last lines of my code (see first problem) to this:
func _physics_process(delta): velocity.y += gravity * delta var desired_velocity = get_input() * max_speed velocity.x = desired_velocity.x velocity.z = desired_velocity.z velocity = move_and_slide(velocity, Vector3.UP) But then it crashes:
Too many arguments for move_and_slide() call. Expected at most 0 but received 2. When I change the @onready to onready, it works, but I will get an error:
Error at (3, 1): Unexpected "Identifier" in class body. My node structure is like this:
Root (Node3D) Collider (CollisionShape3D) Terrain (MeshInstance3D) Player (CharacterBody3D) Body (CollisionShape3D) Feet (CollisionShape3D) Pivot (Node3D) Camera (Camera3D) Light (DirectionalLight3D) (The Root/Player/Pivot node is a Node3D since I don't know how to create a Spatial)
What am I doing wrong and how can I fix these issues?