Dynamic Sprite with Aim attached on his arm

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

Hey guys,

Sorry for disturbing, but maybe someone could help me with another issue. I need dynamic “Sprite” with the “aim projectile” attached to one of his arms and after every click on the target “aim projectile” the “Sprite” would change his posture together with the “aim projectile” attached to his arm. My first question would be, how could I achieve such “Dynamic” sprite and how could I attached my already created and working “Aim projectile”. I hope you are not to busy and will be able to help me out.

EXAMPLE — Imgur: The magic of the Internet

:bust_in_silhouette: Reply From: jgodfrey

So, here’s an outline to accomplish what you want…

  • Create your sprite so it the “arm” is an independent piece
  • Make the “arm” a child of the main sprite, with its pivot point being at the “shoulder” joint (with that, the arm should be able to rotate naturally.
  • Attach an Area2D to the “hand” end of the arm (as a child of the arm sprite)
  • Add a CollisionShape2D as a child of the Area2D. Looking at your picture, you’ll want that to be a big circle. This is what will detect the mouse-clicks.
  • Wire the input_event signal on the Area2D to a script handler. This script will be called when the Area2D is clicked.
  • In that script, rotate your arm as needed.

Here’s a sample scene tree:

 + Body (sprite)
    + Arm (sprite)
      + HitBox (area2d)
        + CollisionShape2D

And a sample input handler script (though it only rotates the arm by 10 degrees in one direction with each click).

func _on_Area2D_input_event(viewport, event, shape_idx):
	if event is InputEventMouseButton and event.pressed and event.button_index == 1:
		$Body/Arm.rotation_degrees += 10

wow jgodfrey, that is a very informative and clear answer, I did not expect that from anyone. Really huge thanks for spending your valuable time for answering my question in such a detailed way. Thanks and have a lovely day.

HashTag | 2020-03-10 14:30

Also, could I ask you how could I make calculations according to position.distance_to(event.position). My previous “Circle” was not moving, so I was able to make calculations based on Area2D, how could I achieve the same with a moving Area2D? My previous calculations were made by taking distance_from_center(where ever user would press inside the circle) and dividing from Collision Radius (my case 260). That’s how I was getting the % and then I am taking % that I got and simple minus 1 (which represents 100%) and multiplying by the max score you can get by a mouse click- 100. By this “formula” I was always getting the right amount of points. How could I modify this into moving area2d and still get the same right output? Thanks in advance

EXAMPLE OF MY CALCULATIONS CODE - Imgur: The magic of the Internet

HashTag | 2020-03-10 15:16