From Test-Scratch-Wiki

Point Towards ()
2.0 Point Towards ().png
Category Motion
Type Stack

The Point Towards () block is a Motion block and a Stack block. The block points its sprite towards the mouse-pointer or another sprite depending on its costume center; this changes the sprite's direction and rotates the sprite.

Example Uses

The Point in Direction () block does not point sprites at specific objects; the Point Towards () block, however, can point sprites at other sprites or the mouse-pointer.

Some common uses for the Point Towards () block:

  • Continuously making a sprite point towards the mouse
when gf clicked
forever
   point towards [mouse-pointer v]
   move (10) steps
  • Guiding a sprite on where to move
point towards [Sprite2 v]
repeat until <touching [Sprite2 v]?>
   move (5) steps
  • Pointing towards a goal in the project
when I receive [Hint v]
point towards [Treasure v]

Workaround

Main article: List of Block Workarounds

This block can be replicated with the following code, which uses trigonometry:

set [delta_x v] to (([x position v] of [sprite 1 v]) - (x position))
set [delta_y v] to (([y position v] of [sprite 1 v]) - (y position))
if <(delta_y) = [0]> then
if <(delta_x) < [0]> then
point in direction (-90 v)
else
point in direction (90 v)
end
else
if <(delta_y) < [0]> then
point in direction ((180) + ([atan v] of ((delta_x) / (delta_y))))
else
point in direction ([atan v] of ((delta_x) / (delta_y)))
end
end

Related Suggestion

A related block has been asked for by Scratchers: point towards x: (0) y: (0) // category=motion[1]
It would be used to point the sprite at a specific pixel.

However, this can be done by placing a sprite at that location and using the Point Towards () block or by using this code:

set [delta_x v] to ((x) - (x position))
set [delta_y v] to ((y) - (y position))
if <(delta_y) = [0]> then
if <(delta_x) < [0]> then
point in direction (-90 v)
else
point in direction (90 v)
end
else
if <(delta_y) < [0]> then
point in direction ((180) + ([atan v] of ((delta_x) / (delta_y))))
else
point in direction ([atan v] of ((delta_x) / (delta_y)))
end
end

The (x) and (y) variables are equivalent to the parameters.

References

See Also

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