How does camera move in 3D space?

Godot Version

v4.6.1

Question

I don’t understand very well how the code below works (it is used to move the camera in the 3D space)

var sensitivity: float = 0.002

func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventMouseMotion:
		rotation.y = rotation.y - event.relative.x * sensitivity
		camera_3d.rotation.x = camera_3d.rotation.x - event.relative.y * sensitivity

It doesn’t move the camera, at least not directly, only rotates it.

Assuming the camera is a child of this node the rotation.y will spin the parent node left/right around the Y axis, this will also rotate any children, so the camera rotates too. If the camera is at an offset the rotation will displace the camera relative to the offset, like when spinning a plate the outer edge covers more distance than the center of the plate.

camera_3d.rotation.x rotates the camera up/down around the X axis.

I don’t understand how the code in the unhandled_input works (rotation.y - event.relative.x, for example)

event.relative represents how many pixels the mouse has moved relative to it’s last position, in both .x for left/right and .y for up/down coordinates.

You can Ctrl+click on any function or property to learn more about it, here’s the docs page for InputEventMouseMotion for example.

1 Like