This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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
Godot version 3.3
in Engine by (378 points)

1 Answer

0 votes
Best answer
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 :)

by (3,906 points)
selected by

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

But I just figured out it doesnt relies on the rotation method the real proplem is that getglobalmouse_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.

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.

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

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