Random movement around

Hello

I have a simple sprite: It’s a red circle.
It has 3 variables as below:

circle_Red

I want to move it randomly and slowly in other directions.
You can see its variables:

and here are the events:

You can see that I limited it so it is not too far from its center in the final step. The problem is without the limitation it works but after adding the limit code, it isn’t working.

What should I do?

A couple of things:

  1. You’re updating First_X_Pos and First_Y_Pos every frame, so the limitation checks will never succeed. Put those two events under an At the beginning of the scene condition.
  2. In the limitation checks, First_X_Pos and First_Y_Pos are not accessed correctly - you’re treating them like functions. Instead, use Circle.Variable(First_X_Pos) and Circle.Variable(First_Y_Pos). This is assuming there is only one Circle object in the scene, otherwise you’ll need to code it differently

I change the variable to what you said but I still can’t limit the range of movement.

You’ve now modified the limitation conditions from ‘>’ and ‘<’ to ‘=’. The values are fine, you just need to change the ‘=’ to the appropriate ‘<’ and ‘>’.

If you use forces, the positions will be decimal values with a large decimal place. The chance of it being exactly 5 pixels from the start is so incredibly tiny it’s non-existent.

As you said I changed the events. But the problem is now the sprite doesn’t move and stops in its first position.
You can see the code below:

Are you still setting the variable Direction to a random value? I can’t see it in the screen snip.

Does it move at all? Or is it stationary from the very beginning?

Yes.

No, yes, it stops.

Is there only 1 circle object in the scene? You can confirm this by looking at the instance list (this icon image on the top right of the editor) and sorting them by name

If there is more than 1, remove the other circle objects.

If there is only 1, can you disable the limiting events, and see if that works? If it does, run the game in preview with debug, and check the value of First_X_Pos and First_Y_Pos in the debugger.

@SaeedP There is an extension for moving objects to random locations if you haven’t looked at it yet.

1 Like

Yes, I know. Thanks.

There is just one.

Yes, it moves But it is far from the center!

Okay, still don’t know how to debug in gDevelop.
I’ll answer you tomorrow.

There is just one.

Yes, it moves But it is far from the center!

Uploading: events5.png…

Okay, still don’t know how to debug in gDevelop.
I’ll answer you tomorrow.

Have you tried using a tween? Set the duration to whatever you want. If it was moving further, I’d calculate the duration based on the distance to keep the speed consistent but this is only 10 pixels.

If you mean TravelToRandom position, I installed it. But couldn’t access it in behavior inspector. I accessed it through the events sheet. It wanted the pathfinder behavior and asked to add it to the object. After I added it, it works but still, I can’t limit the circle movement.

You can use clamp() or min()/max() to set boundaries.

The problem is that the circle gets outside the +/-5 pixel box around the original position, and doesn’t get back. So it’s forces are always stopped. The following addition to the limiting events should fix your problem:

1 Like

Can you please send me the source file?

I’m not at my PC. Add a sprite called Circle. Add the tween behavior to it and these events. If you’re not sure where an expretis just type the first few letters.

Honestly, I did not understand this part.

The code works but the movement of the circle is very strange.