How do I determine the direction of another sprite in Scratch
Asked Answered
D

2

5

I'm programming a simple game for education in MIT Scratch and want to make a sprite turn towards another sprite (think an alien ship following our hero ship). I can easily make the alien ship point towards the hero:

point towards 'hero'

But what I really want to do is something more gradual like this:

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

So how do I determine 'direction of hero'?

Devisee answered 3/3, 2013 at 11:12 Comment(0)
M
6

Unfortunately there doesn't seem to be a built-in way to get this, so some trigonometry is needed. To calculate the direction from sprite 1 to sprite 2 you can calculate the displacement from 1 to 2 in x and y, then use the atan function to find the required angle:

Script to calculate angle to another sprite

Since you actually want the direction relative to the direction the alien ship is facing, it might be better to use the vector product (aka cross product):

enter image description here

The screenshots here are taken from this Scratch project.

Marcel answered 4/3, 2013 at 14:10 Comment(0)
A
5

Use point towards as a way of finding out:

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
Appointed answered 25/4, 2013 at 14:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.