How to control mouse's vector?

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

I am currently creating a top down game for my first project and the character is moving equally to global mouse position. How can I move the mouse back (basically a knock-back effect) every time the player will get hurt as an indication of getting damage? Thank you and sorry for my bad English, I do hope I managed to get my point across.

:bust_in_silhouette: Reply From: exuin

Try using Viewport.warp_mouse().

# at top of script
const KNOCKBACK = Vector2(-20, -20) # or some other value
# in function somewhere
get_viewport().warp_mouse(get_global_mouse_position() - KNOCKBACK)

Hey thank you so much! I got it to work! I will search if I can make it to work together with the angle of how the mouse approaches the trap/obstacle. Have a good one!

This is the effect by the way for those who are wondering:

enter image description here

argames | 2021-01-22 06:59