A sprite following the mouse.

This tutorial will explain how a sprite can be sent to the mouse’s position indefinitely with different variations.

Note Tip: These scripts can make sprites follow other sprites, not just the mouse.

Always Going to the Mouse

This script makes a sprite always be at the location of the mouse:

when green flag clicked
forever
  go to (mouse-pointer v)

The Draggable Sprite Feature can also be used, but this requires the sprite to be dragged around by the player’s mouse. The sprite will remain stationary when not clicked.

Following the Mouse Indefinitely

This script will enable the sprite to trail behind the mouse at a slower, but fixed pace.

when green flag clicked
forever
  point towards (mouse-pointer v)
  move (10) steps


Alternatively, the following script can be used:

when green flag clicked
forever
glide (0.1) secs to x: (mouse x) y: (mouse y)


This allows the trailing object to match the speed of the mouse’s movement to a certain degree.

Following the Mouse if a Boolean is True

This script makes a sprite follow the mouse if the Boolean returns true.

when green flag clicked
forever
  if <...::grey> then
  point towards (mouse-pointer v)
  move (10) steps

Following the Mouse if a Sprite Comes Close Enough

This script will only allow the sprite to follow the mouse when the two objects are within a certain distance of each other.

when green flag clicked
forever
  if <(distance to (mouse-pointer v)) < [100]> then
  point towards (mouse-pointer v)
  move (10) steps

Following the Mouse at a Distance

This script allows the sprite follow the mouse indefinitely, but ensures that the two objects do not come into contact. A buffer distance is set between 50 and 70 to prevent the sprite from jumping or bouncing erratically.

when green flag clicked
forever
  point towards (mouse-pointer v)
  if <(distance to (mouse-pointer v)) < [50]> then
    move (-10) steps
  end
  if <(distance to (mouse-pointer v)) > [70]> then
    move (10) steps
  end
end

Following the Mouse Quicker as the Mouse Moves Away

The speed at which the sprite follows the mouse will increase as the distance between them increases.

when green flag clicked
forever
  point towards (mouse-pointer v)
  move ((distance to (mouse-pointer v)) / (12)) steps
end
Note Note: The number 12 can be raised or lowered to make the sprite follow at a slower or faster pace respectively.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.