Raycast bullet system

Player:

var bullet := preload("res://Scenes/Weapons/Bullet.tscn")
var instance
var player_damage = 10

	# Spawn bullet raycast on Mouse1.
	if Input.is_action_just_pressed("Action1"):
		instance = bullet.instantiate()
		instance.damage = player_damage
		instance.position = camera.global_position
		instance.transform.basis = camera.global_transform.basis
		get_parent().add_child(instance)
		# Make instance damage, player damage.
		instance.damage = player_damage
		# Check stuff.
		instance.do_thing()

Bullet:

@onready var cast = $RayCast3D
var damage

func do_thing():
	if cast.is_colliding():
		var collider = cast.get_collider()
		if collider is Enemy:
			collider.take_damage(damage)
			print("Shoot enemy")
		if collider is StaticBody3D:
			print("Hit wall")

Enemy:

@export var health = 100

func take_damage(damage):
	health -= damage

I’m trying to create a system for my 3d shooter where when the player presses the shoot button, the game creates a raycast which then checks if it hits an enemy or wall. And when I shoot it creates the raycast, but when hitting an enemy or wall it prints nothing which means it is not working.

Could be that the physics process hasn’t had a chance to calculate the collisions yet, because you’re trying to access the collisions at the same time right after instancing the nodes. Try to add this code before checking cast.is_colliding()

await get_tree().physics_frame
await get_tree().physics_frame

Yes, twice the same line, because this signal is emitted at the beginning of the physics frame, so if you want to catch 1 full physics cycle, you need to call it twice.