How can I get a bullet to find the nearest enemy?
id: 749441
category: Help with Scripts
posts: 2
Mar 22, 2024 15:13:10
How can I get a bullet to find the nearest enemy?
view on scratch
view on legacy ocular
Create-Scratch101
loading
I have been wondering for my simple game, How can I get a bullet to find the nearest enemy?Monster Brawl. Find it here.
Mar 22, 2024 16:34:14
How can I get a bullet to find the nearest enemy?
view on scratch
view on legacy ocular
MineTurte
loading
For enemy sprite:I have been wondering for my simple game, How can I get a bullet to find the nearest enemy?Monster Brawl. Find it here.
when green flag clicked
set [ clones] to [0] //Do NOT set this sprite to "this sprite only"
set [ clone] to [0] //SET to "this sprite only"
repeat (50)
add [placeholder] to [distances]
add [placeholder] to [CloneXs]
add [placeholder] to [CloneYs]
end
when I receive [ makeEnemy]
change [ clones] by (1)
create clone of [ myself]
when I start as a clone
set [ clone] to (clones)
forever
replace item ( clone) of [distances] with (distance to [ player])
replace item ( clone) of [CloneXs] with (x position)
replace item ( clone) of [CloneYs] with (y position)
broadcast [ logDistances]
end
when I receive [ logDistances] //Forever run this on clones
set [ slot] to [0]
findClosestDistances
define findClosestDistances // run without screen refresh
set [ currentClosest] to [999]
repeat (length of [ distances] :: list)
change [ slot] by (1)
if <(item ( slot) of [distances] :: list) < (currentClosest)> then
set [ currentClosest] to (item ( slot) of [distances] :: list)
end
end
broadcast [ bullet]
For bullet sprite:
when I receive [ bullet]
go to x: (item ( currentClosest) of [CloneXs] :: list) y: (item ( currentClosest) of [CloneYs] :: list)
Hope this helps!! (Also not sure if this works perfectly but whatever. I already spent like 15 minutes making this lol.)