How to make the camera moves by the mouse in 3D?

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

Could you be more specific please? Add the details in your question description.

SIsilicon | 2019-02-28 00:13

Mouse position:
Either query the mouse events in some controls _input() handler or use get_viewport().get_mouse_position() from i.e. a _physics_process() handler in a script.

Then put the mouse coordinates relative to the viewports size and use the relative difference to the center to add angles to some variables (i.e. rot_x, rot_y). (referring to mouse_x and mouse_y)

Then (in _physics_process()) rotate the cameras transform.basis first (!) around the y-axis (using the rot_x) and then around the x-axis (using rot_y).
(one way to do)

Rotating around the y-axis basically means looking left and right.
Rotating around the x-axis basically means looking up and down. (if you have “up” and “down” which i.e. isn’t the case in 3d space games)

wombatstampede | 2019-02-28 16:34

:bust_in_silhouette: Reply From: sxkod

Look up Jeremy Bullock FPS tutorial on the tube.

If you are talking about using the mouse to change aim/direction of camera, you can do something like this.

var mouse_sens = 0.3
var camera_anglev=0

func _input(event):  		
	if event is InputEventMouseMotion:
		$Camera.rotate_y(deg2rad(-event.relative.x*mouse_sens))
		var changev=-event.relative.y*mouse_sens
		if camera_anglev+changev>-50 and camera_anglev+changev<50:
			camera_anglev+=changev
			$Camera.rotate_x(deg2rad(changev))

If you also want to capture mouse - toggle between
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) and
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

Make sure you release the mouse in which ever function you use to exit.
Good luck