How to determine the direction of another sprite in Scratch

I am programming a simple game for training in MIT Scratch and I want to make a sprite turn in the direction of another sprite (think that an alien ship after our hero ship). I can easily make an alien ship point to a hero:

point towards 'hero'

But I really want to do something more gradual:

if alien direction (in degrees) > direction of hero: turn -2 degrees
if alien direction (in degrees) < direction of hero: turn 2 degrees

So, how to determine the "direction of the hero"?

+5
source share
2 answers

Unfortunately, there is no built-in method for this, so some trigonometry is needed. To calculate the direction from sprite 1 to sprite 2, you can calculate the offset from 1 to 2 in x and y, then use the function atanto find the desired angle:

Script to calculate angle to another sprite

, , , (aka cross product):

enter image description here

Scratch.

+6

:

set temp to direction
point towards hero
if temp > direction
   set direction to temp-2
else if temp < direction
   set direction to temp-2
else
   set direction to temp
+4

All Articles