Roating Sprite to mouse

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

So I want to rotate my weapon to my cursor sprite, which position is set to the global mouse position. It actually works but when I rescale my window to fullscreen for example it doesnt work as before?

How can I rotate my sprite to my cursor right?

Current Code:

    #Update Cursor position
    $Sprites/Cursor.global_position = get_global_mouse_position()
           
	var weapon :Sprite = get_node("Sprites/Weapon")

	var radians = weapon.position.angle_to($Sprites/Cursor.position)
	var degrees = rad2deg(radians) 

    #Update Weapon rotation
	weapon.rotation_degrees = degrees
:bust_in_silhouette: Reply From: timothybrentwood
get_node("Sprites/Weapon").look_at(get_global_mouse_position())

Is what you’re looking for. If this doesn’t work exactly right, rotate the sprite accordingly either in the editor window or in code to offset, like you’ve been doing.

For things like weapons consider making them an Area2D or a KinematicBody2D with a child node being the sprite, so you can get access to collision methods for free :slight_smile:

Thanks for the answer look_at does make the job better then my version, but the proplem is still the same: When I rescale my window to fullscreen it doesnt work correctly, instead it points about 40 degrees over the mouse in some angles

Godot_Starter | 2021-04-29 07:11

But I just figured out it doesnt relies on the rotation method the real proplem is that get_global_mouse_position() doesnt match the mouse position when I am in fullscreen. Maybe because I am making a splitscreen game with two viewports. I am gonna ask in another question.

Godot_Starter | 2021-04-29 07:28

Hmm maybe play around with the different stretch modes/aspects under Project -> Project Settings -> Display -> Window -> Stretch -> Mode/Aspect and see if that helps? I just tried doing the same thing in my own editor and the sprite rotated correctly whether or not the screen was full sized or not.

timothybrentwood | 2021-04-29 19:25

I found out I am only having this issue when I set my stretch mode to either 2d or viewport so the only setting in which everything works correctly is disabled. But this kinda sucks because I have to set it to 2d otherwise you could see way to much in fullscreen

Godot_Starter | 2021-04-29 19:48

Hmm maybe try this modification on your original code:

#Update Cursor position
$Sprites/Cursor.global_position = get_global_mouse_position()

var weapon :Sprite = get_node("Sprites/Weapon")
var global_weapon_position = weapon.to_global(weapon.position)
var radians = global_weapon_position.angle_to($Sprites/Cursor.global_position)
var degrees = rad2deg(radians) 

#Update Weapon rotation
weapon.rotation_degrees = degrees

timothybrentwood | 2021-04-29 21:38