Discuss Scratch

L1B33612A
Scratcher
6 posts

sprite that walks to a random position

ok so im making a game and i need a sprite to be able to walk to a random area, but run into a wall. i need it to bounce off the wall into another wall. ive tried to make it sense a sprite and bounce off of that sprite, but nothing works. im really bad at coding, anything helps.
Apollo_Space25
Scratcher
10 posts

sprite that walks to a random position

You can try

whenclickedforeverglidesecstox:0y:0ifonedge,bounceturndegrees
Coder046
Scratcher
100+ posts

sprite that walks to a random position

If the wall is the edge, use this:
whenclickedsetrotationstyledon't rotatepointindirectionpickrandom-180to180forevermove10stepsifonedge,bounce
As far as I know, “bounce” will not only reset the position of the sprite so that it is not edging off the stage, but it will also bounce the direction.
Detroitman200
Scratcher
14 posts

sprite that walks to a random position

You can use the 3.0 glide to random position block.
deck26
Scratcher
1000+ posts

sprite that walks to a random position

Even if you think you are bad at coding it can be useful for us to see what you've tried so we can nudge you in the rigth direction.
scratch___user12345
Scratcher
1000+ posts

sprite that walks to a random position

Try:

whenclickedforevermove10stepsiftouchingwall?thenrepeatuntilnottouchingWall?pointindirectionpickrandom-180to180v
L1B33612A
Scratcher
6 posts

sprite that walks to a random position

Coder046 wrote:

If the wall is the edge, use this:
whenclickedsetrotationstyledon't rotatepointindirectionpickrandom-180to180forevermove10stepsifonedge,bounce
As far as I know, “bounce” will not only reset the position of the sprite so that it is not edging off the stage, but it will also bounce the direction.

this helps alot, but i want it to bounce off a sprite. thanks for the help! ill do some troubleshooting.
Coder046
Scratcher
100+ posts

sprite that walks to a random position

This does what you want it to. I just came up with this, and it works great!
whenclickedsetrotationstyledon't rotatepointindirectionpickrandom-180to180forevermove10stepsBounceifonedge,bouncedefineBounceThis should be run without screen refresh.settemptorounddirection/90*90"Temp" stands for temporary, and this records the nearest multiple of 90 from the direction.pointindirectiontemp-direction-tempturn180degreesrepeatuntilnottouchingBounce spritemove1steps
Coder046
Scratcher
100+ posts

sprite that walks to a random position

It will have some bugs sometimes, though. I tested using an arrow sprite, and when it collided with one of the sides of the point when going forward, it had to bounce far. Other that things like that, I think that this is at least one of the best solutions available.
Coder046
Scratcher
100+ posts

sprite that walks to a random position

The solution I came up with is shared here.

Powered by DjangoBB