how can i make my hitbox follow my mouse direction but wont go beyond my area2d?

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

how can i make my hitbox position follow my mouse in area2D i set, but if my mouse go outside the area2D i set, my hitbox wont move beyond area2D but still follow the direction?
something like this

:bust_in_silhouette: Reply From: spaceyjase

A few different ways to achieve this. A naive approach is to clamp the hitbox distance from the centre of the area (i.e. its radius). You could also create a polygon collider and do it all in editor.

  • Create a Sprite with a texture of that shape (an image of a circle for example)
  • Press Sprite > Create CollisionPolygon2D Sibling

https://forum.godotengine.org/46417/how-to-prevent-player-from-moving-outside-the-shape-or-area