1
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

7
\$\begingroup\$

On picking your tutorials

You seem to have been mixing Godot 3 and Godot 4 tutorials. So be careful which one the tutorial is for. Chances are that tutorials prior to Godot 4 don't specify.

At the time of writing, in the kidscancode.org website you can tell by the 3.x or 4.x banner on the top right. Also, at the time of writing, the Godot 4 tutorials might be missing parts.

In this case, the following is Godot 3:

  • onready is a keyword.

  • The 3D Node class is called Spatial.

  • The characters in 3D are made with KinematicBody.

  • move_and_slide returns a velocity, and takes a velocity as first parameter among other things. So code looks like this:

    velocity = move_and_slide(velocity, Vector3.UP)
    

And these are Godot 4:

  • @onready is an annotation.

  • The 3D Node class is called Node3D.

  • The characters in 3D are made with CharacterBody3D.

  • move_and_slide takes no parameter, returns if there was a collision (bool) and works with the properties of the CharacterBody3D (such as velocity and up_direction). So code looks like this:

    move_and_slide()
    

Since you are working on Godot 4, the rest of this answer will be only about Godot 4 (although similar concepts apply in Godot 3, the class names are differences).


On the floor collider

The floor problem is the easier to address, although it begets some explanation.

First of all, CollisionShape3D do nothing on their own. They work as children of a CollisionObject3D.

So you need a CollisionObject3D, of which there are a few: Area3D, CharacterBody3D, RigidBody3D, StaticBody3D. IN THIS CASE, you are making a floor, so I would guess you would want an StaticBody3D.

In other context you would need a different kind of CollisionObject3D depending on what you are doing. I cover that with a bit more depth elsewhere.


Thus, modify your scene tree from this:

Root (Node3D)
  Collider (CollisionShape3D)
    Terrain (MeshInstance3D)

To this:

Root (Node3D)
  Floor (StaticBody3D)
    Collider (CollisionShape3D)
      Terrain (MeshInstance3D)

In fact, I would make the MeshInstance3D a sibling:

Root (Node3D)
  Floor (StaticBody3D)
    Collider (CollisionShape3D)
    Terrain (MeshInstance3D)

Pick names that make sense to you, of course.


On the node not found

I don't know what is the issue with the following line:

@onready var camera = $Pivot/Camera

It looks correct to me, assuming it is in the CharacterBody3D in this configuration on the scene tree:

  Player (CharacterBody3D)
    Pivot (Node3D)
      Camera (Camera3D)

Which seems to be the case given the information in the question.

As for making the line work, the best I can suggest is to double check the names, and that you are not getting other errors on the script.

However, I do not see where you are using camera on your script, so you might be able to just remove the line.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .