Document stub.png This article or section may not have content matching Scratch Wiki editing standards. Please improve it according to Scratch Wiki:Guidelines and Scratch Wiki:Editing Conventions. (May 2020)


The tutorial assumes users are comfortable with making projects that;

  • Use keyboard input to move sprites,
  • Send and receive broadcasts,
  • Include custom blocks and
  • Use Game Loops to control and sequence projects.

If a refresher is needed, these tutorials can be followed.

What Does Sprite Detection Mean?

Creating realistic characters in games starts with giving them the ability to sense their surroundings. If a rabbit runs past a carrot and straight up to a cat, does it seem real?

To make games more realistic the rabbit sprite can be created with senses. By sensing other sprites before it touches them, behaviors can be programmed so that the rabbit runs towards the carrot and away from the cat.

This tutorial looks at different methods for sensing changes to the environment around a sprite.

How is it Applied in Games?

Using Sensor Edges

The Rabbit senses the Cat

Some games draw invisible circles that are centered around a sprite, called sensor edges, to help implement character senses. They are as wide as the sprite can sense (hear, see or smell), and that is why it is called an edge.

When the sensor edge is touched by the Cat or a Carrot, the Rabbit can react. The Rabbit can run away from the Cat or towards a Carrot.

One complication is that the Rabbit knows when a Cat or Carrot is at the edge of its senses, but it does not know in which direction. For the Rabbit to know in what direction the Cat or Carrot is, a second step is needed.

Casting Lines to Sense Direction and Location

This process may be called raycasting. This raycasting is when a straight line (like a ray of light) is cast in a possible direction. If the Rabbit casts a ray in the direction it is looking (called a line of sight) and that line touches the Cat, the Rabbit now knows not to go in that direction.

Putting It All Together

Using a sensor edge allows the Rabbit to "smell" the Cat, then it can use raycasting to look around for the Cat before running way, which is a much more realistic behavior for a rabbit.

How Can This be Done in Scratch?

In Scratch, the following two things need to be done.

Collision Detection and Sensor Rings

The sensor ring is a separate sprite. This sensor ring sprite can send messages to the Rabbit whenever a Cat sprite touches it. it also makes sure to always stay at the same position as the Rabbit, so whenever the Rabbit moves, so does the sensor edge sprite.

if <touching (cat v) ?> then
	broadcast (i hear cat v)
go to x: ([x position v] of (Rabbit v)) y: ([y position v] of (Rabbit v))

It follows the Rabbit, and it uses the ghost effect so it is not seen by players in the game, but it still runs its code.

set [ghost v] effect to (100)

Casting Lines With Raycasting

When the Rabbit receives a message (from the sensor ring sprite) telling it a Cat is close, the Rabbit starts looking for the Cat. It uses trigonometry to work out the direction of the cat, so it can turn away from the cat or, to be exact, turn 180 degrees away.

when I receive [I hear a cat v]
raycast ([x position v] of (cat v)) ([y position v] of (cat v)) (0) (180)
point in direction (raycast result)
broadcast (Rabbit sense cat v)

define raycast (target X) (target Y) (offset A) (offset B)
set [delta_y v] to ((target X) - (y position))
set [delta_x v] to ((target Y) - (x position))
if <(delta_y) = (0)> then
	if <(delta_x) < (0)> then
		point in direction (-90)
		point in direction (90)

	if <(delta_y) < (0)> then
		set [raycast result v] to ((offset A) + ([atan v] of ((delta_x) / (delta_y)))
		set [raycast result v] to ((offset B) + ([atan v] of ((delta_x) / (delta_y)))

What Next?

How could raycasting be used if the sensor edge detected a carrot? What behavior would need to change?


This example project shows how to use collision detection to create sprite detection sensors and use trigonometry to find the rotation (direction) between two points in a vector. It also shows you how to make a Rabbit turn away from a Cat.

Want to Learn More?

Check out the wikipedia page on cartesian coordinates

What Can Make it Better?

Here are some ideas to try out.


While rabbits have excellent hearing, not every creature does. Because of this, some games use a cone instead of a circle to simulate human senses. Try to create a project that realistically shows human senses!


Somethings are quieter or harder to see than others. Try to create a project that includes a hawk that silently swoops down on the rabbit! Make sure the rabbit can detect the hawk, but only when it gets close enough.

Handling Obstacles

SandCastleIcon.png This section has links to websites or programs not trusted by Scratch or hosted by Wikipedia. Remember to stay safe while using the internet, as we cannot guarantee the safety of other websites.

Here is a challenge:

People wish they could see through walls sometimes, but it's not fair in a game. How can a game be coded to make sure the Rabbit can't see the cat if its hiding behind a wall?

To find all the points between the rabbit and the cat, have a look at line equations at

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