+2 votes

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

in Engine by (24 points)
recategorized by

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

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)?

Hi Zylann,

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

thnx

s.

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

rotateobject() should drive the rotation

gr.

s.

That's it indeed!

the secret is in the unhandled?

s.

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

much appreciated.
Starting to get the hang of it.

gr.

s.

1 Answer

+2 votes
Best answer

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)
by (27,898 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.