Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » how to target a clone?
- Flame159
-
Scratcher
10 posts
how to target a clone?
hello, and right now i make a tower defense game. how do i make the tower target the the enemy closest to the exit in range?
i know ican use this block to make it in range…
but, how do i make the tower target the the enemy closest to the exit?
all help is appreciated!

i know ican use this block to make it in range…
if <touching [ range ] ?> then
end
but, how do i make the tower target the the enemy closest to the exit?
all help is appreciated!

- nerdloop330
-
Scratcher
100+ posts
how to target a clone?
Well, I guess you could make each enemy record the distance between them and the exit, and make the enemy with the shortest distance record it's location, then make an invisible sprite go to that location, and if the distance between the location and the turret is smaller than the turret's max range, then make the turret target that enemy. It's a lot to put into words, but it's the best advice I can give you. Hope this helps!
- nerdloop330
-
Scratcher
100+ posts
how to target a clone?
Sure, but it may take a while. I'll still try to make it, though.
- -FreeEngines-
-
Scratcher
100+ posts
how to target a clone?
can you make the code too?its pretty simple.
if theres only one tower, you just calculate the positional difference between the tower and the enemie. you can use the pothagoream (do i spell it like that?) algorithm for that
distance = root((a*a+b*b)) in this case, a is the x difference, which you will calculate with abs(xpositionOfTheClone-xPositionOfTheTower) . the y difference will be calculated using (yPositionOfTheClone-yPositionOfTheTower)
both root() and abs() are functions you can find in the operator section, ill just code this and scend a screenshot for potential better understanding
- RT_Borg
-
Scratcher
1000+ posts
how to target a clone?
Hi Flame159,
You might have a look at this demo I've been putting together.
I have multiple Wand clones that each aim at the nearest (to themselves) approaching Wizard clone. So I track Wizard x,y coordinates, rather than the technique nerdloop330 mentioned of having targets build a list of their distances from a shooter.
But you'll see calculating direction to the target points as well as distance. (I don't have a notion of “in range” but I think it would slip in easily enough.) Wizard clones that are struck by a lightning bolt are also able to remove themselves from the targeting lists, so the lists stay the length of the number of active Wizards.
WIP Demo: Wands & Wizards
https://scratch.mit.edu/projects/696018526
(This is more than you need for just a single tower, but if you want your game to expand to multiple towers and multiple targets, you'll probably need something like this.)
I hope this helps. Happy to answer questions.
– RT_Borg
You might have a look at this demo I've been putting together.
I have multiple Wand clones that each aim at the nearest (to themselves) approaching Wizard clone. So I track Wizard x,y coordinates, rather than the technique nerdloop330 mentioned of having targets build a list of their distances from a shooter.
But you'll see calculating direction to the target points as well as distance. (I don't have a notion of “in range” but I think it would slip in easily enough.) Wizard clones that are struck by a lightning bolt are also able to remove themselves from the targeting lists, so the lists stay the length of the number of active Wizards.
WIP Demo: Wands & Wizards
https://scratch.mit.edu/projects/696018526
(This is more than you need for just a single tower, but if you want your game to expand to multiple towers and multiple targets, you'll probably need something like this.)
I hope this helps. Happy to answer questions.
– RT_Borg
Last edited by RT_Borg (June 5, 2022 08:41:34)
- nerdloop330
-
Scratcher
100+ posts
how to target a clone?
Well, I ‘ve finished the code, it’s clumsy and rushed but it'll do. However, @-FreeEngines- idea is more reliable, and if it works, you should use it. Anyway, here's the link:
https://scratch.mit.edu/projects/701200223/
Hope this helps!
P.S. R.T. Borg's idea might be more reliable as well.
https://scratch.mit.edu/projects/701200223/
Hope this helps!
P.S. R.T. Borg's idea might be more reliable as well.
- Flame159
-
Scratcher
10 posts
how to target a clone?
Well, I ‘ve finished the code, it’s clumsy and rushed but it'll do. However, @-FreeEngines- idea is more reliable, and if it works, you should use it. Anyway, here's the link:
https://scratch.mit.edu/projects/701200223/
Hope this helps!
P.S. R.T. Borg's idea might be more reliable as well.
wait, if i have multipe towers, then the tower can shoot the enemy in another towers range how to fix this?
- RT_Borg
-
Scratcher
1000+ posts
how to target a clone?
In the demo I shared, each Wand clone is deciding which direction to point based on which Wizard clone is closest to that Wand.
Also, I just put a comment in the Wand code where you could check that a candidate wizard distance is less than some maximum allowed range.
– RT_Borg
Also, I just put a comment in the Wand code where you could check that a candidate wizard distance is less than some maximum allowed range.
– RT_Borg
- Flame159
-
Scratcher
10 posts
how to target a clone?
In the demo I shared, each Wand clone is deciding which direction to point based on which Wizard clone is closest to that Wand.
Also, I just put a comment in the Wand code where you could check that a candidate wizard distance is less than some maximum allowed range.
– RT_Borg
thanks!
- Discussion Forums
- » Help with Scripts
-
» how to target a clone?



