Invalid get index 'global_transform' (on base: 'null instance')

Godot Version

4

Question

Hello everyone,

I am currently working working on getting my player to move relative with the camera, but I am getting this error message and my gam will not run whenever I try to test the game. This is my code so far.

</
class_name Player extends CharacterBody3D

@export_category(“Player”)
@export_range(1, 35, 1) var speed: float = 10 # m/s
@export_range(10, 400, 1) var acceleration: float = 100 # m/s^2

@export_range(0.1, 3.0, 0.1) var jump_height: float = 1 # m
@export_range(0.1, 3.0, 0.1, “or_greater”) var camera_sens: float = 1

var jumping: bool = false
var mouse_captured: bool = false

var gravity: float = ProjectSettings.get_setting(“physics/3d/default_gravity”)

var move_dir: Vector2 # Input direction for movement
var look_dir: Vector2 # Input direction for look/aim

var walk_vel: Vector3 # Walking velocity
var grav_vel: Vector3 # Gravity velocity
var jump_vel: Vector3 # Jumping velocity

@onready var camera: Camera3D = $Camera

func _ready() → void:
capture_mouse()

func _unhandled_input(event: InputEvent) → void:
if event is InputEventMouseMotion:
look_dir = event.relative * 0.001
if mouse_captured: _rotate_camera()
if Input.is_action_just_pressed(“jump”): jumping = true
if Input.is_action_just_pressed(“quit”): get_tree().quit()

func _physics_process(delta: float) → void:
if mouse_captured: _handle_joypad_camera_rotation(delta)
velocity = _walk(delta) + _gravity(delta) + _jump(delta)
move_and_slide()

func capture_mouse() → void:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
mouse_captured = true

func release_mouse() → void:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
mouse_captured = false

func _rotate_camera(sens_mod: float = 1.0) → void:
camera.rotation.y -= look_dir.x * camera_sens * sens_mod
camera.rotation.x = clamp(camera.rotation.x - look_dir.y * camera_sens * sens_mod, -1.5, 1.5)

func _handle_joypad_camera_rotation(delta: float, sens_mod: float = 1.0) → void:
var joypad_dir: Vector2 = Input.get_vector(“cam_left”,“cam_right”,“cam_up”,“cam_down”)
if joypad_dir.length() > 0:
look_dir += joypad_dir * delta
_rotate_camera(sens_mod)
look_dir = Vector2.ZERO

func _walk(_delta: float) → Vector3:
move_dir = Input.get_vector(“move_left”, “move_right”, “move_forward”, “move_backwards”)
var _forward: Vector3 = camera.global_transform.basis * Vector3(move_dir.x, 0, move_dir.y)
var walk_dir: Vector3 = Vector3(_forward.x, 0, _forward.z).normalized()
walk_vel = walk_vel.move_toward(walk_dir * speed * move_dir.length(), acceleration * _delta)
return walk_vel

func _gravity(delta: float) → Vector3:
grav_vel = Vector3.ZERO if is_on_floor() else grav_vel.move_toward(Vector3(0, velocity.y - gravity, 0), gravity * delta)
return grav_vel

func _jump(delta: float) → Vector3:
if jumping:
if is_on_floor(): jump_vel = Vector3(0, sqrt(4 * jump_height * gravity), 0)
jumping = false
return jump_vel
jump_vel = Vector3.ZERO if is_on_floor() else jump_vel.move_toward(Vector3.ZERO, gravity * delta)
return jump_vel

/>

1° When you put your code here, select all the code and use the code formmating button (</>) to organize it, otherwise will be hard to read your code.

2° Show which line is throwing the error.


That said, i suppose this error comes from here:

move_dir = Input.get_vector(“move_left”, “move_right”, “move_forward”, “move_backwards”)
var _forward: Vector3 = camera.global_transform.basis * Vector3(move_dir.x, 0, move_dir.y)
var walk_dir: Vector3 = Vector3(_forward.x, 0, _forward.z).normalized()

Tha means your camera variable is null (don’t reference any node) so will not work.

You set camera here:

var grav_vel: Vector3 # Gravity velocity
var jump_vel: Vector3 # Jumping velocity

@onready var camera: Camera3D = $Camera

Are you sure that’s not $Camera3D? Check if the name of node is correct and the node path matches. You can also drag the node to the code editor, that will put the correct node path in code:

Apologies, I was trying to find out how people did the code formatting, but I did not see anything on the post menu that seemed to have come close to how others were doing it.

To answer the second point, it is saying line 48 is the problem which is
“camera.rotation.y -= look_dir.x * camera_sens * sens_mod”

My advice still valid in this case, check if the node path to camera is correct, that means you’re not passing the correct node path when you initialize camera and this variable keeps null. Also show the scene related to the bug, that can help understand what’s going wrong.

Here is a picture of the level’s scene. If you need any other scenes I can provide a picture of those too.

Your node path is wrong, your camera node is child of another node and you need to take that into account to pass the correct node path and even if wasn’t a child of other node, you put the wrong node name (forgot the “3D” in the end).

Try:
@onready var camera: Camera3D = $Marker3D/Camera3D

You also can drag the node into the code editor and that will automactly paste the correct node path.


And last, i noticied you edited your post but you misunderstood what i said, the formatting buttuon i said is that one:

image

2 Likes

Thank you explaining all of this to me! After some reading and a bit of problem solving I can test my game again.

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