Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Rotate until it points to Specific Sprite
- patrice789
-
12 posts
Rotate until it points to Specific Sprite
Hello…I need help on this
I know how to make a sprite point towards another object
However, I would like to know is how would you make it so that the object rotates until it is pointed at that other object?
I know how to make a sprite point towards another object
However, I would like to know is how would you make it so that the object rotates until it is pointed at that other object?
- DanloVorje
-
100+ posts
Rotate until it points to Specific Sprite
How much trigonometry do you know?
One way to do this without trig is to give a sprite a “targeting dot” with a script like this:
Make sure that TargetAngle (I usually call it something shorter like TargDir or TargetQ is a global variable if you're using this method. The sprite you want to turn will then rotate depending on whether its direction is greater or lesser than the desired angle.
The problem with this is that you need a separate targeting dot sprite for each sprite that you want to turn; this can get very messy, and there's no way to reconcile it with cloning.
On the other hand, if you know some trigonometry, you can have clones that independently target a non-clone sprite.
For this method, you'll have to compute the differences of x and y coordinates between the sprite and the one you want it to point towards, and then use
You'll then need a script to tell the sprite to turn in a certain direction depending on whether its current direction is greater or lesser than the desired angle. You may end up with some problems with the sprite turning around abruptly when the target angle switches at +/-180, but there are ways around that. I'd start with the basic turn and work from there.
This project uses the targeting dot method (the targeting dots are named Pointer# and correspond with Enemy#): http://scratch.mit.edu/projects/10699354/
This project uses the trig method for a bunch of clones (the clones also have angular momentum, so it's a bit more complicated): http://scratch.mit.edu/projects/17726664/
Hope this helps! I'll be watching this thread if you have more questions.
–Danlo
One way to do this without trig is to give a sprite a “targeting dot” with a script like this:
Make sure that TargetAngle (I usually call it something shorter like TargDir or TargetQ is a global variable if you're using this method. The sprite you want to turn will then rotate depending on whether its direction is greater or lesser than the desired angle.
The problem with this is that you need a separate targeting dot sprite for each sprite that you want to turn; this can get very messy, and there's no way to reconcile it with cloning.
On the other hand, if you know some trigonometry, you can have clones that independently target a non-clone sprite.
For this method, you'll have to compute the differences of x and y coordinates between the sprite and the one you want it to point towards, and then use
To figure out a desired angle. Keep in mind that arctangent provides a value between -90 and 90, so you'll have to have separate equations depending on whether the target sprite is above or below (since Scratch uses 0 = up) the sprite you want to turn.
You'll then need a script to tell the sprite to turn in a certain direction depending on whether its current direction is greater or lesser than the desired angle. You may end up with some problems with the sprite turning around abruptly when the target angle switches at +/-180, but there are ways around that. I'd start with the basic turn and work from there.
This project uses the targeting dot method (the targeting dots are named Pointer# and correspond with Enemy#): http://scratch.mit.edu/projects/10699354/
This project uses the trig method for a bunch of clones (the clones also have angular momentum, so it's a bit more complicated): http://scratch.mit.edu/projects/17726664/
Hope this helps! I'll be watching this thread if you have more questions.
–Danlo
Last edited by DanloVorje (March 21, 2014 02:53:03)
- TheLogFather
-
1000+ posts
Rotate until it points to Specific Sprite
This project might have just what you want:

http://scratch.mit.edu/projects/18248372
In particular, check out the “spin towards direction” custom block.
(Note: it's designed to be run frame-by-frame from within a forever or repeat loop, as happens in the “when I receive main loop” script just to left of that block's definition…)

http://scratch.mit.edu/projects/18248372
In particular, check out the “spin towards direction” custom block.

(Note: it's designed to be run frame-by-frame from within a forever or repeat loop, as happens in the “when I receive main loop” script just to left of that block's definition…)
Last edited by TheLogFather (March 21, 2014 09:14:15)
- scubajerry
-
1000+ posts
Rotate until it points to Specific Sprite
Are the objects clones or sprites? Hello…I need help on this
I know how to make a sprite point towards another object
However, I would like to know is how would you make it so that the object rotates until it is pointed at that other object?
Look at the ARCTAN2 custom block in this script. If it is a clone, you will need to capture the X?Y yourself. If it s a sprite, you can just use the \
In either case, the variable ARCTAN2 RESULT is the direction to the thing you are interested in. You can them just use a loop (like @turkey3 said) to rotate until you get to that direction.
Last edited by scubajerry (March 21, 2014 10:18:33)
- bitom2007
-
63 posts
Rotate until it points to Specific Sprite
THANKS, @Turkey3 SO MUCH, this helped me a lot with my project. I've been spending my whole night doing this!

- deck26
-
1000+ posts
Rotate until it points to Specific Sprite
No need to necropost on a 5 year old topic just to thank someone though! THANKS, @Turkey3 SO MUCH, this helped me a lot with my project. I've been spending my whole night doing this!
- Discussion Forums
- » Help with Scripts
-
» Rotate until it points to Specific Sprite