0 votes

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 --- https://imgur.com/JOSP0Wo

in Projects by (29 points)

1 Answer

+1 vote
Best answer

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
by (9,092 points)
selected by

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.

Also, could I ask you how could I make calculations according to position.distanceto(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 distancefrom_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 - https://imgur.com/dScPfqf

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 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 webmaster@godotengine.org with your username.