Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Make a sprite follow another sprite
- DontKiIIMe
-
100+ posts
Make a sprite follow another sprite
How can I make a sprite follow another sprite? By follow, I mean go to the other sprite in a second and not/barely touching it.
Like when I person is following you, if you go to X, the other person also goes to X.
Like when I person is following you, if you go to X, the other person also goes to X.
- MegaApuTurkUltra
-
1000+ posts
Make a sprite follow another sprite
when gf clickedIn case that was cut off, here's the source
forever
repeat until <((((x position)-([x position v] of [target v]))*((x position)-([x position v] of [target v])))+(((y position)-([y position v] of [target v]))*((y position)-([y position v] of [target v])))) < ((distance to stay from target)*(distance to stay from target))>
point towards [target v]
move (5) steps
end
end
when gf clicked
forever
repeat until <((((x position)-([x position v] of [target v]))*((x position)-([x position v] of [target v])))+(((y position)-([y position v] of [target v]))*((y position)-([y position v] of [target v])))) < ((distance to stay from target)*(distance to stay from target))>
point towards [target v]
move (5) steps
end
end
What this does is
- Checks it's distance to the target sprite by using the Pythagorean theorem. However, to save some time (because square roots are slow), instead of using distance=sqrt(a*a+b*b), I check by squaring distance. Since y=x^2 is increasing for x>0, we can assume that if a>b, a^2>b^2. So, we use (distance to stay away)^2 > (((my x)-(other x))^2 + ((my y)-(other y))^2)
- If it's still too far away, point toward the target and move 5 steps. Adjust the “5” to how fast you want the sprite to follow the target sprite.
HTH
Last edited by MegaApuTurkUltra (Nov. 18, 2014 16:26:58)
- shoresbeep
-
1000+ posts
Make a sprite follow another sprite
glide (1) secs to x: ([x-position v] of [ Sprite1 v]) y: ([y-position v] of [ Sprite1 v])
- DontKiIIMe
-
100+ posts
Make a sprite follow another sprite
when gf clickedIn case that was cut off, here's the source
forever
repeat until <((((x position)-([x position v] of [target v]))*((x position)-([x position v] of [target v])))+(((y position)-([y position v] of [target v]))*((y position)-([y position v] of [target v])))) < ((distance to stay from target)*(distance to stay from target))>
point towards [target v]
move (5) steps
end
endSet (distance to stay from target) as a bit more than half your target sprite's width or height, whichever looks betterwhen gf clicked
forever
repeat until <((((x position)-([x position v] of [target v]))*((x position)-([x position v] of [target v])))+(((y position)-([y position v] of [target v]))*((y position)-([y position v] of [target v])))) < ((distance to stay from target)*(distance to stay from target))>
point towards [target v]
move (5) steps
end
end
What this does is
- Checks it's distance to the target sprite by using the Pythagorean theorem. However, to save some time (because square roots are slow), instead of using distance=sqrt(a*a+b*b), I check by squaring distance. Since y=x^2 is increasing for x>0, we can assume that if a>b, a^2>b^2. So, we use (distance to stay away)^2 > (((my x)-(other x))^2 + ((my y)-(other y))^2)
- If it's still too far away, point toward the target and move 5 steps. Adjust the “5” to how fast you want the sprite to follow the target sprite.
HTH
There are still some problems…
- The target (the player) can move around, so when you're moving and suddenly walk in the other direction (+180°), the sprite who's following, ‘teleports’ to the other side of the target…
- And also, the distance to stay away from the target is ‘big’, even if I change it to 2…
Hope you can help me out

- DontKiIIMe
-
100+ posts
Make a sprite follow another sprite
glide (1) secs to x: ([x-position v] of [ Sprite1 v]) y: ([y-position v] of [ Sprite1 v])
I want that the sprite who's following doesn't touch the target (the player)…
- MegaApuTurkUltra
-
1000+ posts
Make a sprite follow another sprite
Here I made an example project: http://scratch.mit.edu/projects/34965254/
I fixed the teleporting rotation bug, and added slowing down when the following sprite gets close to the target (luckily y=x^2 already has the right curve so no addition calculation was necessary except scaling down so the follower doesn't move too fast).
I fixed the teleporting rotation bug, and added slowing down when the following sprite gets close to the target (luckily y=x^2 already has the right curve so no addition calculation was necessary except scaling down so the follower doesn't move too fast).
- shoresbeep
-
1000+ posts
Make a sprite follow another sprite
Then maybe try this:
glide (1) secs to x: ([(([x-position v] of [player v]) - (1)) v] of [ Sprite1]) y: ([(([y-position v] of [player v]) - (1)) v] of [ Sprite1])
Last edited by shoresbeep (Nov. 18, 2014 19:23:41)
- Cyoce
-
500+ posts
Make a sprite follow another sprite
when gf clicked
forever
wait until <(distance to [target v]) > (desired distance)>
point towards [target v]
move (5) steps
end
- DontKiIIMe
-
100+ posts
Make a sprite follow another sprite
http://scratch.mit.edu/projects/34965254/Here I made an example project:
I fixed the teleporting rotation bug, and added slowing down when the following sprite gets close to the target (luckily y=x^2 already has the right curve so no addition calculation was necessary except scaling down so the follower doesn't move too fast).
I still got another problem with the distance to stay away from the target, I set the distance to 50 in your project and it seems to be close enough, but whenever I set the distance to 50 in my project, it isn't the same distance in your project… I also tried to set the distance to 10, but the follower always keeps a big distance from the target… You think you can help me out? (again


EDIT: I fixed it, the center of the follower wasn't in the middle of the follower


Last edited by DontKiIIMe (Nov. 22, 2014 10:11:49)
- MegaApuTurkUltra
-
1000+ posts
Make a sprite follow another sprite
Glad to hear that! Good luck with your game!http://scratch.mit.edu/projects/34965254/Here I made an example project:
I fixed the teleporting rotation bug, and added slowing down when the following sprite gets close to the target (luckily y=x^2 already has the right curve so no addition calculation was necessary except scaling down so the follower doesn't move too fast).
I still got another problem with the distance to stay away from the target, I set the distance to 50 in your project and it seems to be close enough, but whenever I set the distance to 50 in my project, it isn't the same distance in your project… I also tried to set the distance to 10, but the follower always keeps a big distance from the target… You think you can help me out? (again)
EDIT: I fixed it, the center of the follower wasn't in the middle of the follower. Anyway, if you read this, I'm really thankful for your help
- fanonscratch12
-
41 posts
Make a sprite follow another sprite
Support:
forever
point towards [Target v]
move (10) steps
end
- rusldelyon
-
9 posts
Make a sprite follow another sprite
But fanonscratch that looks too choppy and plus you need waiting before that 

- Harakou
-
1000+ posts
Make a sprite follow another sprite
Considering the age of this thread and how the OP has already said their question was answered, I'm going to close this thread. Please don't bump old, already resolved threads. Thanks!
- Discussion Forums
- » Help with Scripts
-
» Make a sprite follow another sprite