How to drive rotation of an object based on mouse drag?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Nonius3d
:warning: Old Version Published before Godot 3 was released.

Hi all,

I’m trying to drive the rotation of an object depending on the drag of the mouse. Not only should the motion drive the rotation but also the direction of the drag (left/right) should set the direction of the rotation.

For some reason I don’t fin the right angle/hook to tackle it .

searched the q&a for things like orbit or turntable and did the same on google but no clues came up.

so thnx for your help on showing me in the right direction.

gr.

sven

Guess I post this in the wrong place. can it be moved? thnx

Nonius3d | 2016-03-18 13:34

Which kind of rotation are you looking for? Do you want to keep the vertical axis of the object (yaw+pitch) like Google Maps, or not (yaw+pitch+roll)?

Zylann | 2016-03-18 14:01

Hi Zylann,

Yaw over up axis will do (it’s a 3D object).

thnx

s.

Nonius3d | 2016-03-18 14:04

this gives me updated mouse cordinates:

func _input(event):
# var pressed = false -- temp
if(event.type == InputEvent.MOUSE_BUTTON and event.is_pressed()):
	var mouse_position_Clicked = event.pos
	print("Mouse clicked at :" + mouse_position_Clicked)
elif(event.type == InputEvent.MOUSE_MOTION):
	var mouse_position_moved = event.pos
	print("Mouse position at :" mouse_position_moved)
	_rotate_object()
pass

_rotate_object() should drive the rotation

gr.

s.

Nonius3d | 2016-03-18 14:51

That’s it indeed!

the secret is in the unhandled?

s.

Nonius3d | 2016-03-18 15:01

_input would be fine too, depending on your game: http://docs.godotengine.org/en/latest/tutorials/engine/inputevent.html

Zylann | 2016-03-18 15:07

much appreciated.
Starting to get the hang of it.

gr.

s.

Nonius3d | 2016-03-18 15:08

:bust_in_silhouette: Reply From: Zylann

Is this what you are looking for?

extends TestCube


var pressed = false
var last_position = Vector2()


func _ready():
	set_process_unhandled_input(true)
	

func _unhandled_input(event):
	if event.type == InputEvent.MOUSE_BUTTON:
		pressed = event.is_pressed()
		if pressed:
			last_position = event.pos
	elif event.type == InputEvent.MOUSE_MOTION and pressed:
		var delta = event.pos - last_position
		last_position = event.pos
		self.rotate_y(-delta.x * 0.01)