Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How do we know the distance with the distance block
- moutamou
-
Scratcher
80 posts
How do we know the distance with the distance block
Hello im making a strategic game, and i want to make that if an ennemy is near ===> shoot him, so i want to use the 
(distance to [ v])block which can be really useful, but i don't know how we can actually know how much it is, can someone help me?

- Nezon
-
Scratcher
1000+ posts
How do we know the distance with the distance block
([sqrt v] of ((((x :: grey) - (x position)) * ((x :: grey) - (x position))) + (((y :: grey) - (y position)) * ((y :: grey) - (y position))))
- moutamou
-
Scratcher
80 posts
How do we know the distance with the distance block
Can you explain? Thank you

Last edited by moutamou (Dec. 10, 2021 20:40:19)
- The_Imaginarium
-
Scratcher
1000+ posts
How do we know the distance with the distance block
The distance means the amount of pixels away from an object.
- moutamou
-
Scratcher
80 posts
How do we know the distance with the distance block
The distance means the amount of pixels away from an object.yes i know but i don't want to count all the pixels to make a range
- joaolmkimbr
-
Scratcher
16 posts
How do we know the distance with the distance block
I didn't understand your last post, but going off your first one, to know if an enemy is inside a range of the weapon, suposing there's only one thing that shoots, you can put a script like this inside the enemy sprite:
if (distance to (NAME OF THE THING THAT SHOOTS)<(DESIRED RANGE IN PIXELS)) than:
DO SOMETHING
or, depending on the case, put inside the weapon:
if (distance to (NAME OF THE ENEMY)<(DESIRED RANGE IN PIXELS)) than:
DO SOMETHING
To decide the DESIRED RANGE IN PIXELS, you can just try different values.
Suposing there are multiple enemies and multiple weapons, than you'll need to use a list to store the positions of the enemies and calculate the distance to the weapons using:
Sqrt(((ENEMY.X-WEAPON.X)*(ENEMY.X-WEAPON.X))+((ENEMY.Y-WEAPON.Y)*(ENEMY.Y-WEAPON.Y)))
instead of the distance to block.
If you didn't get how to detect this, could you share the project link so that I can take a look at it and explain for the specific case?
if (distance to (NAME OF THE THING THAT SHOOTS)<(DESIRED RANGE IN PIXELS)) than:
DO SOMETHING
or, depending on the case, put inside the weapon:
if (distance to (NAME OF THE ENEMY)<(DESIRED RANGE IN PIXELS)) than:
DO SOMETHING
To decide the DESIRED RANGE IN PIXELS, you can just try different values.
Suposing there are multiple enemies and multiple weapons, than you'll need to use a list to store the positions of the enemies and calculate the distance to the weapons using:
Sqrt(((ENEMY.X-WEAPON.X)*(ENEMY.X-WEAPON.X))+((ENEMY.Y-WEAPON.Y)*(ENEMY.Y-WEAPON.Y)))
instead of the distance to block.
If you didn't get how to detect this, could you share the project link so that I can take a look at it and explain for the specific case?
Last edited by joaolmkimbr (Dec. 10, 2021 21:01:19)
- joaolmkimbr
-
Scratcher
16 posts
How do we know the distance with the distance block
Oh, sorry, I didn't get the question the first time I saw it, I tried making a project to visualize the area the distance covers < https://scratch.mit.edu/projects/615110891/ > just enter a certain distance and it'll show you the range it covers. It may lag a little bit, but it's because it's using run without screen refresh.
Last edited by joaolmkimbr (Dec. 10, 2021 21:11:45)
- The_Imaginarium
-
Scratcher
1000+ posts
How do we know the distance with the distance block
If the object you want to shoot is less than a certain distance away, you should make the turret shoot. That creates a radius of covered space.The distance means the amount of pixels away from an object.yes i know but i don't want to count all the pixels to make a range
- ventoaurum
-
Scratcher
100+ posts
How do we know the distance with the distance block
so you don't want it to be exact? i dont get why you would want thatThe distance means the amount of pixels away from an object.yes i know but i don't want to count all the pixels to make a range
- moutamou
-
Scratcher
80 posts
How do we know the distance with the distance block
Oh, sorry, I didn't get the question the first time I saw it, I tried making a project to visualize the area the distance covers < https://scratch.mit.edu/projects/615110891/ > just enter a certain distance and it'll show you the range it covers. It may lag a little bit, but it's because it's using run without screen refresh.this guy understood me, it's exactly what i wanted thank you so much

- gipaty
-
Scratcher
29 posts
How do we know the distance with the distance block
so you don't want it to be exact? i dont get why you would want thatThe distance means the amount of pixels away from an object.yes i know but i don't want to count all the pixels to make a range
when green flag clicked
forever
if <(distance to [ v]) = [...]> then
turn cw (15) degrees
endend
Last edited by gipaty (Dec. 12, 2021 08:48:03)
- Discussion Forums
- » Help with Scripts
-
» How do we know the distance with the distance block