(Redirected from Distance (Sensing value))

Distance to ()
distance to ( v)
Category Sensing
Type Reporter

The Distance to () block is a Sensing block and a Reporter block. The block reports the Euclidean distance, in pixels, between it and the mouse-pointer or a specified sprite's costume center, even if the specified sprite is hidden.

The Scratch Cat saying its distance from the toad.

If there is nothing in the drop-down insert of the block, or if a sprite that was deleted is still in the drop-down insert, it reports the distance as 10,000. In Scratch 2.0 for v432 and earlier, it reports 0 instead.

This block is case-sensitive.

Example Uses

As this block can give distances between objects, it is very useful in projects that require a great deal of careful sensing and movement.

Some common uses for the Distance to () block:

  • Estimating when collisions will occur
if <(distance to (snowball v)) < (40)> then
say [Watch out!]
wait until <touching (snowball v)?>
say [] // stop saying the message
stop [all v]
  • Helping determine how long it should take a sprite to move somewhere, judging from the distance
set [movements v] to ([ceiling v] of ((distance to (meteor v)) / (10))) // "movements" being the amount of 10 pixel motions to reach the meteor
  • Determining how far a rocket should fire
set [real distance v] to (join ((distance to (asteroid v)) * (10000)) [ kilometers]) // for relating pixel distance to real life
  • Detecting distances from an invisible sprite and displaying the score as a game
change [score v] by ((1800) - ((10) * (distance to (bullseye v))))
  • Making a variable change when you move the sprite around from another sprite
set [safety v] to ((100) - ((distance to (house v)) / (3))


Main article: List of Block Workarounds

This block can be replicated with the following code:

([sqrt v] of ((((x position) - ([x position v] of (Wanted Sprite v))) * ((x position) - ([x position v] of (Wanted Sprite v)))) + (((y position) - ([y position v] of (Wanted Sprite v))) *((y position) - ([y position v] of (Wanted Sprite v))))))

Related Suggestion

Some Scratchers want a block that gives the distance to any location, not just a sprite.[1] It would be like this:

(distance to x: () y: ():: sensing)

However, it can be replicated with a similar workaround:

([sqrt v] of ((((x position) - (wanted x)) * ((x position) - (wanted x))) + (((y position) - (wanted y)) * ((y position) - (wanted y)))))

Alternatively, a sprite can be moved to the wanted location:

//Inside sprite1:
define distance to x: (x) y: (y)
set [send x v] to (x)//These are needed because custom blocks are sprite local, so the x and y blocks cannot go to another sprite without breaking.
set [send y v] to (y)
broadcast (move v) and wait
set [result v] to (distance to (Move sprite v))

//Inside Move sprite:
when I receive [move v]
switch costume to (0x0 px v)
change size by ((size)+(((x)+(2))*((x)+(2)))
switch costume to (500x500 px v)
go to x: (send x) y: (send y)
switch costume to (0x0 px v)


Cookies help us deliver our services. By using our services, you agree to our use of cookies.