Follow mouse in 3D?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Stoozey

New to GDScript, I want to make a 3D object follow the mouse on one axis.
I’ve searched for a while now and really can’t find any answers so I’m asking here. Thank you!

If by follow, you mean to to it’s position, you gotta use navigation. I won’t describe that, read the documentation about it. As for getting the position of the cursor in 3d, you gotta raycast from the camera. I advise you to read up about recasting for that

KijeviGombooc | 2019-07-23 10:42

:bust_in_silhouette: Reply From: Siegel

not sure how correct this is but this is how I did it, was only trying to map x axis

func _input(event):
	if event is InputEventMouseMotion:
		var mouse_position = get_viewport().get_mouse_position()
		var new_pos = self.get_viewport().get_camera().project_position(mouse_position, self.translation.y)
		move_and_collide(Vector3(new_pos.x-self.translation.x, 0, 0))