Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How to make a turret shoot at a clone?
- ht12048
-
New Scratcher
3 posts
How to make a turret shoot at a clone?
Hi I have tried looking at other topics asking this but the answers don't have enough explanation or anything. Does anyone know an easy way to point at the closest clone? If you do can you please explain in depth. Thanks. http://scratch.mit.edu/projects/50979476/ is the project im trying to do this for.
- deck26
-
Scratcher
1000+ posts
How to make a turret shoot at a clone?
If you only have one shooter you could give each clone a number and have a list containing the distances using the
(distance to [shooter v])block. Then, in a custom block, find the lowest distance. So, for example, item 5 in the list is the distance from clone 5 to the shooter.
- ht12048
-
New Scratcher
3 posts
How to make a turret shoot at a clone?
If you only have one shooter you could give each clone a number and have a list containing the distances using the(distance to [shooter v])block. Then, in a custom block, find the lowest distance. So, for example, item 5 in the list is the distance from clone 5 to the shooter.
Do you know what I would put in that block? And I am new to lists so how would they work?
- deck26
-
Scratcher
1000+ posts
How to make a turret shoot at a clone?
I recommend looking at http://wiki.scratch.mit.edu/wiki/List if you're new to lists.
To give each clone a number you need a local (for this sprite only) variable for the sprite. Let's call it CloneID.
So that's the first stages
To give each clone a number you need a local (for this sprite only) variable for the sprite. Let's call it CloneID.
when green flag clickedwould create 10 clones, the first would have CloneID=1, the next CloneID=2 etc.
delete (all v) of [distances v] // get rid of the list's contents so we can restart. The list needs to be global (visible to all sprites)
set [CloneID v] to [1 ]
repeat (10)
create clone of [myself v]
change [CloneID v] by (1)
end
when I start as a clonewould keep the distances up to date.
add [0] to [distances v] // make an item in the list for this clone
forever
replace item (CloneID) of [distances v] with (distance to [player v])
end
So that's the first stages
- deck26
-
Scratcher
1000+ posts
How to make a turret shoot at a clone?
Then for the custom block you go through the list of distances to find the smallest value.
So how does the turret know where that clone is? One option would be to have global x and y variables that a clone will change if it's the current one. Then get the original sprite to stay hidden but go to those coordinates and point the turret at the sprite.
So at the end of the green flag script in the earlier post add
and have an addition clone script
I think that does it.
define shortestwould set ‘whichclone’ to the clone ID for the nearest clone.
set [shortest v] to [500 ]
set [index v] to [ 1]
repeat (length of [distances v])
if <(item (index) of [distances v]) < [ shortest]> then
set (shortest) to (item (index) of [distances v])
set (whichclone) to (index)
end
change (index) by (1)
end
So how does the turret know where that clone is? One option would be to have global x and y variables that a clone will change if it's the current one. Then get the original sprite to stay hidden but go to those coordinates and point the turret at the sprite.
So at the end of the green flag script in the earlier post add
hide
forever
go to x: (globalx) y: (globaly)
end
and have an addition clone script
when I start as a clone
forever
if <(whichclone) = (CloneID)> then
set [globalx v] to (x position)
set [globaly v] to (y position)
end
end
I think that does it.
- ht12048
-
New Scratcher
3 posts
How to make a turret shoot at a clone?
So would these scripts be put into the sprite I want to aim at, or the turret that aims at them?
- Scrachrulz
-
Scratcher
42 posts
How to make a turret shoot at a clone?
Put them into the sprite that is being cloned (and aimed at). Then add a “show” block at the start of one of the “when I start as a clone” headers, and tell the turrent to forever point toward the other sprite. @deck26 great script, by the way! I actually didn't know that local variables create a new instance of themselves for each clone; I'll use that in the future!
- deck26
-
Scratcher
1000+ posts
How to make a turret shoot at a clone?
So would these scripts be put into the sprite I want to aim at, or the turret that aims at them?The green flag script that creates clones has to be in the sprite being aimed at or the local cloneID variable won't work. Obviously ‘when created as sprite’ scripts have to be for the same sprite.
The main thing to avoid with the custom block is running it for multiple clones as it looks at them all anyway. So it could be in a forever loop for the turret or in the green flag script for the other sprite - the latter, just before the goto x,y should work.
As @Scrachrulz points out I should have included a show at the start of one of the ‘when started as clone’ scripts.
- dbossinger
-
Scratcher
19 posts
How to make a turret shoot at a clone?
But how do you get the turret to aim at the clones?
- jokebookservice1
-
Scratcher
1000+ posts
How to make a turret shoot at a clone?
This is a good tutorial. I won't be using it in any of my projects though I am using it in my masters of steam series, if you want to check it out.This is both spam and a necropost. Please be aware that spamming is reportable.
But how do you get the turret to aim at the clones?The easiest method is to create a hidden sprite that uses the Go to X: Y:, and then use (point towards ), but it can also be done with trigonometric functions.
Just to repeat: Please do not spam or necropost. This topic is almost a year old.
- Zarif973442050
-
Scratcher
23 posts
How to make a turret shoot at a clone?
I am making a game based off Warfame's shooter game, and I personally think that the easiest way to do it is to make the turret have two parts; 1: The base of the turret, and 2 the turret's barrel itself. Make it so that the barrel of the turret always goes to the base, and make it point at the zombie. In my case, I would say (sry can't put the code blocks, my mac is being trash rn) point towards zombie, and create a clone of laserbullet. This makes it so that the turret points at the zombie clone (which is treated as the zombie) and shoots at it too! I hope that solves your problems. (I know there are a lot better and complicated ways, but I'm just starting out).
- Discussion Forums
- » Help with Scripts
-
» How to make a turret shoot at a clone?