0 votes

Hi,

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:

https://imgur.com/a/wxSI9lx

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:

https://imgur.com/a/3QE2yTx

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.

Godot version latest
in Engine by (59 points)

Please log in or register to answer this question.

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.