Discuss Scratch

mrlitzinger
Scratcher
5 posts

Wandering enemies on an RPG scroller.

I am making an RPG and have come to a speed bump. I want to make enemies and NPC's wander in a small area while scrolling with the background as my character moves around. I have tried moving randomly mixed with the normal scrolling blocks different ways but the sprite just tends to move at a 45 degree angle. Is there a way to achieve this? I hope this has not been answered before, the F.A.Q. would not let me search for some reason.
StrangeMagic32
Scratcher
1000+ posts

Wandering enemies on an RPG scroller.

do you have a myX and myY type thing for the NPCs? If not you'll want those, then you can just do this.
ifpickrandom1to10=1thenifpickrandom1to5=1thenturnpickrandom-10to10degreeschangexbydistance*sinofdirectionchangeybydistance*cosofdirectionifnottouchingwall?thenchangemyXbydistance*sinofdirectionchangemyYbydistance*cosofdirectiongotox:myX-camXy:myY-camY

Last edited by StrangeMagic32 (Dec. 12, 2019 23:44:19)

mrlitzinger
Scratcher
5 posts

Wandering enemies on an RPG scroller.

This is the simplest way I found after posting my question. This allows a sprite to wander in an RPG like game while staying in the same area. The only problem is the delay of 1 sec, the sprite wanders with the main character sprite for a bit as it tries to look for its original position. Cutting down the glide time keeps him closer to his origin but then he is jumping around too fast. Is there a way to clean up this bit of code or add a small bit to get him to remain in his “box”?

I am trying to get the code down to the simplest form I can because I am working with 3rd-5th graders for an event where they will be timed.


whenclickedforeverglide1secstox:scrollx+pickrandom90to110y:scrolly+pickrandom140to160

Last edited by mrlitzinger (Dec. 22, 2019 16:20:49)

StrangeMagic32
Scratcher
1000+ posts

Wandering enemies on an RPG scroller.

mrlitzinger wrote:

This is the simplest way I found after posting my question. This allows a sprite to wander in an RPG like game while staying in the same area. The only problem is the delay of 1 sec, the sprite wanders with the main character sprite for a bit as it tries to look for its original position. Cutting down the glide time keeps him closer to his origin but then he is jumping around too fast. Is there a way to clean up this bit of code or add a small bit to get him to remain in his “box”?

I am trying to get the code down to the simplest form I can because I am working with 3rd-5th graders for an event where they will be timed.


whenclickedforeverglide1secstox:scrollx+pickrandom90to110y:scrolly+pickrandom140to160
I see, I'll try to help.
deck26
Scratcher
1000+ posts

Wandering enemies on an RPG scroller.

Think of a scroller as being a window onto the real world. Your enemies need to move smoothly in real world coordinate terms. So work out the current real world x and y and apply scrollx/y as corrections to get the screen position. That is all that is going on when a backdrop moves to scrollx plus some multiple of 480 - the multiple of 480 is the real world position and scrollx is an adjustment to get the screen position.
mrlitzinger
Scratcher
5 posts

Wandering enemies on an RPG scroller.

StrangeMagic32 wrote:

mrlitzinger wrote:

This is the simplest way I found after posting my question. This allows a sprite to wander in an RPG like game while staying in the same area. The only problem is the delay of 1 sec, the sprite wanders with the main character sprite for a bit as it tries to look for its original position. Cutting down the glide time keeps him closer to his origin but then he is jumping around too fast. Is there a way to clean up this bit of code or add a small bit to get him to remain in his “box”?

I am trying to get the code down to the simplest form I can because I am working with 3rd-5th graders for an event where they will be timed.


whenclickedforeverglide1secstox:scrollx+pickrandom90to110y:scrolly+pickrandom140to160
I see, I'll try to help.



Thank you. Not sure why I didn't get notified of your response.
mrlitzinger
Scratcher
5 posts

Wandering enemies on an RPG scroller.

deck26 wrote:

Think of a scroller as being a window onto the real world. Your enemies need to move smoothly in real world coordinate terms. So work out the current real world x and y and apply scrollx/y as corrections to get the screen position. That is all that is going on when a backdrop moves to scrollx plus some multiple of 480 - the multiple of 480 is the real world position and scrollx is an adjustment to get the screen position.



Thank you for your response. I can get the sprite to move easily with the background smoothly but getting the sprite to move around a small area while moving smoothly with the background is the issue.
StrangeMagic32
Scratcher
1000+ posts

Wandering enemies on an RPG scroller.

https://scratch.mit.edu/projects/255541420/
The “NPC” sprite has a wandering script that works. It uses Pythagorean Theorem to check distance and what not. (I know that not many 3rd-5th graders know it, but it's a simple way to do it) (also uses some other math to get the diagonal movement)

Last edited by StrangeMagic32 (Dec. 25, 2019 03:55:45)

DragonzSamurai21
Scratcher
14 posts

Wandering enemies on an RPG scroller.

StrangeMagic32 wrote:

https://scratch.mit.edu/projects/255541420/
The “NPC” sprite has a wandering script that works. It uses Pythagorean Theorem to check distance and what not. (I know that not many 3rd-5th graders know it, but it's a simple way to do it) (also uses some other math to get the diagonal movement)

I did this and it worked great but I am using it for my enemies so the enemies wander. Can you make it so when you get close the enemy chases after you and when you are to far away from it, it goes back to wandering. Thank you.

https://scratch.mit.edu/projects/428346530/

Powered by DjangoBB