trying to make my character follow the mouse

Godot Version

Question

The error means that it can’t find a node at the path $Camera. That means that there is no node called “Camera” that is a child of the node the script is on. Check your spelling, capitalization, and hierarchy.

1 Like

If you have a 3D game, then try this script (listed below). He finds the mouse position (using the camera), and moves to it. I can throw off a simple guide to this, if you don’t understand anything.

Script:

extends CharacterBody3D

const SPEED = 5
var target_position = null

func _physics_process(delta):
	if Input.is_action_pressed('mouse_left'): # Mouse position
		var viewport := get_viewport()
		var mouse_position := viewport.get_mouse_position()
		var camera := viewport.get_camera_3d()
		var origin := camera.project_ray_origin(mouse_position)
		var direction := camera.project_ray_normal(mouse_position)
		var ray_length := camera.far
		var end := origin + direction * ray_length
		var space_state := get_world_3d().direct_space_state
		var query := PhysicsRayQueryParameters3D.create(origin, end)
		var result := space_state.intersect_ray(query)

		target_position = result["position"]

	if target_position != null: # check
		var direction = target_position - global_position # Use global position
		direction = direction.normalized()

		# Rotate the character (mesh) towards the target using look_at
		var char = $KnightHeadB
		char.look_at(-target_position, -Vector3.UP)
		char.rotation.x = 0
		char.rotation.z = 0

		velocity = direction * SPEED
		velocity.y = 0

		if position.distance_to(target_position) <= 0.1:
			target_position = null
			velocity.y = 0
			velocity.x = 0
			velocity.z = 0

	move_and_slide()

i found ur reddit post with a bigger image, the problem is that ur camera is called Camera3D and not Camera like in the code

1 Like