About alternating (move,stop and repeat) movement workflow, use of animation or render loop

Hi, I have a spherical planet with a player and an enemy. The enemy has a parent that act as a pivot. To move the enemy I rotate the pivot and so the enemy rotate following the surface of the planet.
Now, I would like to move the enemy in an alternating pattern (i.e. move for a bit, then stops for some time, and then move again), I found a bad and non-elegant solution that works more or less like this:

IF enemy moved for less then threshold:
move for another bit
moved distance+=bit
moved distance=0

This actually works but it’s really impractical since with more enemies I should save for every enemy the moved distance, the time to the new movement …

I don’t know which could be the better solution to solve my problem. i took a look at animations,and there are nice ways to set up keyframes, but it seems that you animate a property of the object, while I use the function rotate.
Also in the future I would like to use the function locallyTranslate() while executing the animation.

Can someone help me?
Here is a playground with a minimal setup and my (horrible) solution:

Adding @DarraghBurke to have a look

I would advise you to create an enemy class to encapsulate its state this way all the enemy instances could take care of the state and even have different setup which will make your code easy to scale.

1 Like

Thank you, good solution! I’ll try this for now :slight_smile:


I agree with @sebavan, an easy way to scale up your solution is to create a class that will handle everything about a single enemy: creating the mesh, updating the position, deciding when to move and when not to move, etc. Then, to create more enemies, you can just instantiate the class.

As far as alternating the moving vs. stopping, I think you have the right idea, but if you want more complex behavior, you might want to look into finite state machines. Your question is basically about how to manage game state, so you might find this article useful.


Thank you for the answer, I will look into that :slight_smile: