Object Inspection Mechanic

Godot Version

4.6.1

Question

Hi! Trying to figure out this 3d object inspection mechanic. So far I got everything working very nicely, the only thing i would like to make better is the mechanic to rotate the object on itself while holding cmnd. Right now the mouse works only on the y axis and rotates the object, while i would like to be able to draw circles around the object and make it rotate basically looking at the mouse. I’ve messed around with the basis but can’t figure out how to use look_at properly. It’s easier to understand if you try out the code for yourself, so I’ll leave the snippet here.


extends Node3D

@export var object : Node3D
var friction : float = 2
var is_rotating : bool = false
var rotating_on_itself : bool = false
var object_velocity : Vector3 = Vector3.ZERO

func _ready() -> void:
	pass # Replace with function body.

func _process(delta: float) -> void:
	object.rotate_x(object_velocity.y * 0.2 * delta)
	object.rotate_y(object_velocity.x * 0.2 * delta)
	object.rotate_z(object_velocity.z * 0.2 * delta)
	object_velocity = object_velocity.lerp(Vector3.ZERO, friction * delta)

func _input(event: InputEvent) -> void:
	if Input.is_action_pressed("RotateObject") and event is InputEventMouseMotion:
		object_velocity = Vector3(event.relative.x, event.relative.y, 0)
		if Input.is_action_pressed("RotateOnZAxis"):
			var vp = get_viewport()
			var mouse_location = 1 if vp.get_mouse_position().x < vp.get_visible_rect().size.x/2 else -1
			object_velocity = Vector3(0, 0, event.relative.y * mouse_location)

I’m also attaching the scene tree

Basically the Node3D InspectItemScene references and rotates the InspectedObject. You can use any mesh in the MeshInstance3D.

Any help in figuring this out would be greatly appreciated!!