Accessing a nested node in 3d

Godot Version

Question

Hello, this is my first project in Godot and I’m new to Gamedev and programming. Sorry about my last two posts, just figured out why nothing was being displayed. How do I access Camera3D in the enemy script?

Specifically for the following line of code:

var result = space_state.intersect_ray(PhysicsRayQueryParameters3D.create($box.global_position, camera_node.global_position, 1, [self]))

Player Script variable declarations:

@onready var neck = $neck
@onready var head = $neck/head
@onready var standing_collision_shape = $Standing_Collision_Shape
@onready var crouching_collision_shape = $Crouching_Collision_Shape
@onready var ray_cast_3d = $RayCast3D
@onready var camera_3d = $neck/head/eyes/Camera3D
@onready var eyes = $neck/head/eyes
@onready var animation_player = $neck/head/eyes/AnimationPlayer ``

Enemy Script:

extends CharacterBody3D

enum States {
	patrol,
	chasing,
	hunting,
	waiting
}
@onready var box = $box


var currentState : States 
@onready var navigation_agent_3d = $NavigationAgent3D
@export var waypoints : Array[Marker3D]
var waypointIndex : int
@export var patrolSpeed = 2
@export var ChaseSpeed = 3
@onready var patrol_timer = $PatrolTimer
@export var crouching_speed = 10

var playerInEarshotFar : bool
var playerInEarshotClose : bool
var playerInSightFar : bool
var playerInSightClose : bool
var Player 

var patrolTimer : Timer 



 func _ready():
	
	currentState = States.patrol
	Player = get_tree().get_nodes_in_group("player")[0]
	
	navigation_agent_3d.set_target_position(waypoints[0].global_position)


func CheckForPlayer():
	var space_state = get_world_3d().direct_space_state
	
	#ERROR HERE
	var result = space_state.intersect_ray(PhysicsRayQueryParameters3D.create($box.global_position, camera_node.global_position, 1, [self]))
	
if result.size() > 0:

Also, I don’t fully understand the logic yet so if I’m missing crucial information, please tell me and I will update this post.

The camera is declared on the Player node, so it should be :
var result = space_state.intersect_ray(PhysicsRayQueryParameters3D.create($box.global_position, Player.camera_3d.global_position, 1, [self]))

what the error said?

Thanks for the answer. However, using it gives this error:

Invalid get index ‘Camera3D’ (on base: ‘Node3D’).

The error said: Parser Error: Identifier “camera_node” not declared in the current scope.

I tried creating a variable at the top but that received the same error.

@onready var camera_3d = $"../player/neck/head/eyes/Camera3D"

how

this called?

it possibly the camera is not ready yet, but you called it from the enemy

if that’s the case, try add await get_tree().physics_frame before the var result

For both of these solutions I get this error:

Invalid get index ‘camera_3d’ (on base: ‘Node3D’).

well yeah, since you made it become ../player/neck/head/eyes/Camera3D then the Player. is no need anymore
var result = space_state.intersect_ray(PhysicsRayQueryParameters3D.create($box.global_position, camera_3d.global_position, 1, [self]))

you can always print(camera_3d.global_position), before the var result to test it the camera_3d you set path with, onready, actually referencing the right node and given its global position

Thank you for helping me. It works!!!

1 Like