I've been trying to create a system where, depending on the location and angle of the player's mouse position relative to the sprite, the right or left arm is selected and is able to rotate.
Like in this gif:
The code is use is basically along the lines of:
If mouse is closer to left arm or right arm.
Select arm for rotation. (and how rotated the mouse is from the player) by using this line:
var angle = rad2deg(self.global_position.angle_to_point(get_global_mouse_position()))
if angle >= 10 or angle <=-10:
`arms_state = ARMS_STATE.ROTATE`
Rotate by using following function:
func armrotatetoward(arm,location: Vector2):
arm.rotation = lerp_angle(arm.rotation,arm.global_position.angle_to_point(get_global_mouse_position()),0.7)
This function is ran in the main loop of the node.
The issue I have is that, if the player is rotated, when the angletopoint() finds an angle, it is as if the player's rotation is still 0. I think this is because angletopoint() uses the global origin plane of the scene rather than the plane the player is rotated at.
This gif tried to show whats going on:
By the time the player has rotated 180 degrees, it is as if the arm control is completely inverted (holding mouse right side of character will move left arm, and vice versa).
I have tried alerting this line
var angle = rad2deg(self.globalposition.angletopoint(getglobalmouseposition()))
to this line:
var angle = rad2deg(self.global_position.angle_to_point(get_global_mouse_position()))+self.global_rotation_degrees
Which has helped me a little, but it hasn't fixed the issue properly.
Does anyone know what I need to do to make angletopoint take into account the rotation of a node?
Massive thanks in advance.