I would really like to change/tweak something to make it so when I click mouse to use the tool that my player sprite will forcibly face the direction clicked with the mouse (up down left right maybe if mouse is close in range of player if possible) pretty much just like in Stardew Valley when you click up down left or right around you to till the ground, it flips the sprite / plays the "tilling animation" as seen in one of the screenshots for the right direction where the mouse was clicked. Sorry if this is explained terribly! I'm pretty new and majority of this code in the screenshots is from me following a big tutorial, this is mostly me really wanting to learn this new thing that's not included in tutorial and for me to know more about it and use in the future while using some of the code that's already here if it is in fact possible. Maybe it'd just be better to learn later with maybe a better/easier to add into base when I do something seperate on my own? Any advice appreciated thank you 🩷
#2D top down making player face where mouse is clicked if able? / Interested in how in general
2 messages · Page 1 of 1 (latest)
You can calculate the direction between the mouse_position and player.global_position using angle_to: https://docs.godotengine.org/en/stable/classes/class_vector2.html#class-vector2-method-angle-to
Internally it uses a maths function called atan2 if you want to look up the maths side of it
As you only have limited directions it's probably easiest to just use a if statement based on the angle to pick the new direction. So you would end up with something like
angle = player.global_position.angle_to(mouse_position)
if (angle < PI / 4 && angle > -PI / 4)
// Face up
else if ...
You can print the angle to get an idea of how it changes if you're not used to radians
A 2D vector using floating-point coordinates. Description: A 2-element structure that can be used to represent 2D coordinates or any other pair of numeric values. It uses floating-point coordinates...