0 votes

https://imgur.com/a/TydnsgC <= Example

Hey guys,
I want to create a circle as a target 2d and being able to catch the mousepressedon on that circle. If it is pressed in the middle it should give max amount of points per hit, let's say 100, if it is pressed away from the center (inside the circle) it should give x points depending on the distance from the center point, if outside = 0points.
I am pretty new to the Godot and I don't know how to solve this issue. I have no script for this scene yet and I am not sure if I should use CollisionShape2D or something else for the circle and etc. I would appreciate it if anyone could help it out and guide me in solving this issue. Thanks, fellas.

in Engine by (29 points)

1 Answer

+3 votes
Best answer

Use an Area2D with a CollisionShape2D set to "Circle". Area2D has input capability - connect its input_event signal, and check for mouse click events.

Next, measure the distance from the click position to the area's position (the center of the circle), and assign your points.

func input_event(viewport, event, shape_index):
    if event is InputEventMouseButton and event.pressed:
        var distance_from_center = position.distance_to(event.position)

Alternatively, you could add multiple CollisionShape2D circles of varying radius, and use the shape_index property to see which one was clicked, using that to assign points.

by (20,479 points)
selected by

Thanks for your answer, I am getting the distance from the center! Amazing :) Maybe you know the syntax of getting the center? Because I want to assign some points to the very center of the Area2D. var center = ?
Thanks in advance!

The center of the collision shape is its position. But this will probably be (0, 0) if you call it on the collision shape since it's relative to the parent. Use the area's position for that. Or if you need global coordinates, global_position.

I see, Thanks a lot for your time explaining it to me! Have a nice weekend mate

Sorry to disturb you, but maybe you 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 placement of target 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

If anyone will need the answer to this question - https://godotengine.org/qa/63775/dynamic-sprite-with-aim-attached-on-his-arm

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.