Discuss Scratch

ewanat
Scratcher
1 post

Trying to get one player sprite to follow another?

Hey there! I'm currently working on a Earthbound/Deltarune style RPG in scratch and I need some help. I really want my player 2 sprite to follow behind my player 1 sprite, but I have absolutely no idea how to code this in a way that doesn't completely break everything. I found an old old tutorial on this forum from 2014 that I was able to use to get the P2 sprite itself to glide and follow the P1 at an optimal speed and distance I wanted, but because this is a 32 bit style RPG the code I found wasn't working with the established directions and frame animations I had established, so it looked weird. I'm linking the project below here, including all of the wacky, messy stuff I've been trying today. The P1 movement and control code is exactly how I want it, and I want the P2 to move exactly like that, just delayed. If anyone has any insight as to how I do this, please please let me know!!!

https://scratch.mit.edu/projects/830936818
souleymane2
Scratcher
100+ posts

Trying to get one player sprite to follow another?

Hi there

Your project looks rly nice and i really love the astethic your going for. I cant wait to see the finished result!

About your issue:

Instead of creating a full own working follow code, i'd offer making smt that looks like your P2 is following while it dosnt. If you want, i could work on a better follow code and share it to you.

Heres what i propose:

Whenever a key is touch, two variables are modified a P2xChange and a P2yChange. Then, when editing the movement of P2, you set him to his location plus P2xChange and P2yChange.

//in P1 sprite
when green flag clicked
forever
if <key [up v] pressed?> then
set [P2xChange v] to [0]
set [P2yChange v] to [-10]
end
end

// in P2 sprite

when [tick v] key pressed
go to x: ((x position) + (P2xChange)) y: ((y position) + (P2yChange))

Whenever we move the up key, we want player 2 to follow player 1 from below. So we set our P2xChange to 0 and our P2yChange to -10. That way, upon positioning P2, itll look like hes following player 1. If this is done with every direction, it should look like P2 is following P1

If you have any other issues please let me know
10data10
Scratcher
100+ posts

Trying to get one player sprite to follow another?

souleymane2 wrote:

Whenever we move the up key, we want player 2 to follow player 1 from below. So we set our P2xChange to 0 and our P2yChange to -10. That way, upon positioning P2, itll look like hes following player 1. If this is done with every direction, it should look like P2 is following P1

I'm pretty sure that this will cause P2 to mirror P1 instead of following it as soon as you change directions.

I made a snake game where I wanted to segments to all look different so each clone had to follow the one in front. The same thing is useful with sprites and I think would apply to your project.
This looks a little strange because it's a Snap example but it's basically Scratch with a custom reporter block.



So, I used local variables to store the x and y position of the leader before it moves and then let the follower move to that location.
Cactusgamer55
Scratcher
100+ posts

Trying to get one player sprite to follow another?

So you want player 1 to follow player two? Then do this.

glide (0.5) secs to Sprite2
souleymane2
Scratcher
100+ posts

Trying to get one player sprite to follow another?

10data10 wrote:

souleymane2 wrote:

Whenever we move the up key, we want player 2 to follow player 1 from below. So we set our P2xChange to 0 and our P2yChange to -10. That way, upon positioning P2, itll look like hes following player 1. If this is done with every direction, it should look like P2 is following P1

I'm pretty sure that this will cause P2 to mirror P1 instead of following it as soon as you change directions.

I made a snake game where I wanted to segments to all look different so each clone had to follow the one in front. The same thing is useful with sprites and I think would apply to your project.
This looks a little strange because it's a Snap example but it's basically Scratch with a custom reporter block.



So, I used local variables to store the x and y position of the leader before it moves and then let the follower move to that location.

Assuming this were right then there is another method

If you just store the old position of P1 and apply to P2 whenever a key is pressed he would follow the player

Although your code might work, for the sake of a game like that, often times it is better to use simpler alternatives that wont work the same on a bigger scale (say a snake game) but will work just fine for your project that just needs one sprite following another

Powered by DjangoBB