Spawn npc ю where i look

Godot Version

4.2

Question

How can i do, if I use v (npc event) in position where i look spawn scene enemy?

Raycast from the camera to get a world position and spawn an enemy at collision point.

can you sen’t a script, i can’t do a script for it

Heres a function (attach it to camera) that will give you a position in 3d space wherever you click

func shootRay():
	# Get dictionary of information on whatever player clicked
	var mousePos = get_viewport().get_mouse_position()
	var rayLength = 1000
	var from = project_ray_origin(mousePos)
	var to = from + project_ray_normal(mousePos) * rayLength
	var space = get_world_3d().direct_space_state
	var rayQuery = PhysicsRayQueryParameters3D.new()
	rayQuery.from = from
	rayQuery.to = to
	var result = space.intersect_ray(rayQuery)

	# Parse dict if not empty
	if !result.is_empty():
		return result["position"]

i have ray and this is script:

extends Camera3D


@onready var ray = $RayCast3D

func _physics_process(delta):
	if ray.is_colliding():
		print("collided")

how i can get intersection point and spawn the scene there?

@onready var scene = load("<Pathtoyourscene>")
@onready var loaded = scene.instantiate() 

and later

loaded.position = shootRay()

$"<Nodepath>".add_child(loaded)

make sure to chage the scene path and the node you want to spawn it as a child of. you should be able to do get_tree() for the root

Also, You shouldn’t use $RayCast3D. Its much slower and the function I sent earlier “talks” directly to the physics engine

Unless you need to get it to spawn in the middle of your camera i suppose. Nevermind in that case

Please look at target position in the documentation