Random movement for sprite

I would like to create a firefly (a simple sprite) that randomly moves along x and z axis in a given range… I tried with this method i founded in another post (Has anyone tried to use UMG with the oculus yet? - XR Development - Epic Developer Community Forums) but my sprite start moving forward, even outside the radius, without changing direction.

Hi ,

From looking at the blueprint you linked, I believe the reason it’s moving forward is because the radius is always having it’s origin changed to the new location of the firefly. I would suggest setting a vector variable to be used as the origin in the “Get Random Point in Radius” node that is only set on construction (Or on BeginPlay) to the actor’s location. This way it’ll constantly be getting points based off an area around the same origin.

Hope this helps!

Hi ,

Was my answer able to help you solve this issue? If not, please let me know and I’ll be happy to assist you further.

It doesnt help… I created a character (firefly) with a AI controller done as you said but the sprite doesnt move at all. Could you explain me exactly how to make a 2d firefly to randomly move in radius?

Use the blueprint that you were originally using that worked, you don’t need to create a new one. Make the modifications to make it look like the following screenshot:

Edit: I forgot to set the radius to an amount, please change that to the value you wish you use, as 0 won’t give you any different points.

Out of curiosity, I tried this out myself. It seems that ‘Get Random Point in Radius’ is constantly returning 0,0,0. I’m not sure how your original screenshot worked, but I’m looking into the problem.

It seems like the ‘Get Random Point in Radius’ node requires a Nav mesh to generate it’s points. Seeing as you wish to move along the X and Z axis, this won’t be useful for your application. I would suggest doing this with math to calculate the random points. You can find more information about what type of math to use at the following link:

I hope this helps!

Looking at what you linked i found this method to move my firefly randomly in range but it happens instantly… how can i make it do it gradually?

It choose a random point in x and z axis and move to it every four seconds, but the transformation is instant.

Hello ,

I saw your question and I had an example similar to what you need made up in a blueprint that I have been testing. The only difference is that it works in 3D space. You should be able to set one of the axes to 0 in order to use it in 2D. I hope that this helps.

Example:

In this example I have taken an actor blueprint and I have added a static mesh to it. This mesh simulates physics. I then made two vector variables that I wanted to use to represent where the ball (my static mesh) currently is and where I want it to go. The math that is included basically provides a set of parameters for where I want my random locations to be chosen from. The radius variable determines the size of a cube in space of which valid points can be taken from. From there I find the distance between the origin of the “cube” and the newly pick location (chosen at random). I then compare this distance to that of the radius variable and as long as it is within the chosen radius will provide a valid point within a sphere. Ultimately this chooses a random point with in a sphere shape area around the origin of the actor. This new valid point is used to set the vector that determines where I want my ball to head to next. The other vector is simply given the current location of the actor every other frame. From there I subtract the old location of the actor from the new location that I want which then provides the velocity needed to move the ball to the new location. Two things should be noted. I have used the add velocity option to get a nice curve when changing direction. This also required me to use a velocity damping node to keep the velocity in check.

Make it a great day

I found a way to do it simply by making a lerp with a timeline. Now it works perfectly!

I’m glad to hear you found a way to do what you wanted to do. Can you elaborate on how you managed to make it work so that future users can see the solution?