Move a textureRect based on Mouse Position

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

I’m working on a title screen and was wondering how I could smoothly move a sword icon based on mouse position on the screen.
I want to be able to move the sword with a relative mouse position

Anybody know what I should do to acheive this, I only want to move it a bit (10-35 units along the x-axis).

I’m not looking for player movement, I want to create an effect where a sword icon moves back and forth in the x-axis only for the title screen based on mouse position.

Moldor2 | 2020-09-29 21:26

The link shows how to get mouse position and how to use that to move a player. Simply apply the same kind of logic on your sword instead of the player.

Bernard Cloutier | 2020-09-30 13:33

:bust_in_silhouette: Reply From: unlut

Add a TextureRect node as a direct child to your title screen scene. In your title screen’s script, move it to your mouse position:

onready var mouseFollowerRect = $TextureRect  #  node with sword image

#  _process function of your title screen
func _process(delta):
    var currentMousePos = get_global_mouse_position()
    var offsetVector = Vector2(30, 10)
    var finalTexturePos = currentMousePos + offsetVector

If you want a smoother follow instead of directly snapping texture to current mouse position, you can use tweens.

I’m not the OP, but I’m really thankful for this answer. I plan to make a bullet hell where the mouse pointer is the target, and i needed to set up an area node that would follow the cursor and handle the damage system.