My player is rotating in a weird and buggy way when the mouse cursor comes near to it

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By i_am_dhrutiman1999

var look = get_global_mouse_position()
look_at(look)

this code takes care of the rotation of the player based on the position of mouse cursor…I tried with local_position of mouse but that makes things worse

Define “weird and buggy”: what exactly is happening? If you can, provide a video or even better an minimal example project that reproduces the problem.

njamster | 2020-09-13 20:56

:bust_in_silhouette: Reply From: maxwellc055

Hi!

I don’t do much using mouse movement, but the it seems that your best bet would be using “relative”. This returns the mouse position relative to its previous position.
An example would be:

var speed = (however fast you want to turn)
func _input(event):
        if event is InputEventMouseMotion:
            rotate.y(-event.relative.x * speed )

I think this will help get rid of your jitter. Calibrate your speed variable by trail and error and it should feel pretty good. Using rotate.y and event.relative.x assumes your character is rotating around its y axis according to the horizontal movement of the x axis. If this isn’t true, adjustments will need to be made, but the code should still be essentially correct.

I hope I could be of some assistance to you,
Maxwell