+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.



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



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)

rotateobject() should drive the rotation



That's it indeed!

the secret is in the unhandled?


_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.



1 Answer

+2 votes
Best answer

Is this what you are looking for?

extends TestCube

var pressed = false
var last_position = Vector2()

func _ready():

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 (29,090 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 Frequently asked questions and 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 [email protected] with your username.