Targeting enemies with click

Godot Version

4.5.1-steam

Question

Hello! I’m here because I have no idea how to solve a problem. I’m new to Godot and to coding, so a “ForFools” explanation will be much appreciated, but if it’s not possible, a general idea is welcome as well

Context:
I’m trying making a simple space game and I want to make a targeting system by click, lets say you have a random spawned enemy on scene, you want to attack it so you click on it, it gets locked/targeted and with another function attack it(I’ll worry about the attack system later but for now I need to understand this) and I want to be able to change target even if the first one wasn’t destroyed

I have my character, movement, and stats, also I have an enemy with basic AI that follows the player within range, also I have an targeting animation that activates when clicked on

But I don’t know how to properly target that enemy or activate and deactivate the target animation when clicked on others, so I come here seeking for knowledge, a brief guide, some hints or steps to follow. or the mention of tools I haven’t discovered.

I tried with emith signals, input events in area2D, and the most I could do was that the player faced the target, but it faced the global position once, and if we move, I have to click the enemy again to get the new global position.

My basic question is:
-How do I target and lock an enemy?

With that, I think the path to follow is to answer the next questions:
-How do I store the global position of the enemy, so, when I click on it the player is always facing the enemy
-How do I change target, from one spawned enemy to another, or to another thing in space

But if there is a better way to do it I’ll love to learn know and learn.

If you need more info, let me know and I’ll answer ASAP, but my code right now is just basic stuff, movement, stats, and details to make it look nice, but I think nothing of it is related to the problem

PD: I speak Spanish, so many of my variables are Spanish words, sorry :c
PDD: a couple of pics to show you what I did with the emit signals


Thank you so much for your attention and help in advance

After clicking on the enemy don’t send the global position of the enemy once but instead send the enemy itself and store where ever needed. That way you can always access the current global position.

1 Like

OMG how I didn’t think on that, I forgot I can store the node as a variable D: it worked like a charm, Thank you so much!

1 Like