Running in Circles: Creating Circular Movement in Game Objects

In this article I will cover how to create a circular movement for an enemy. The exact behavior shown in the gif has the enemy travel down the screen until it reaches a certain point and once it reaches that point it will move around in a circle.

Lets give this enemy a random starting position on the X and for the Y will initialize it to 8 so it is off screen.

We want to declare an bool variable that will track when the enemy is finished moving down the screen, so we can start the circular movement. We set this variable to true in the Start.

In the Update we want to check when the enemy has reach a certain Y position on the screen and for me that was 2. While the enemy’s Y position is greater than or equal to 2 and _isMovingDownn = true, we want to move the enemy down using translate, Vector.down multiplied by a speed variable and Time.deltaTime.

We want to capture the current x position and y position. So, we declare float variables.

When the enemy’s Y position moves passed that stop point, which is 2 and _isMovingDown is true we will store the current X and Y coordinates in the corresponding variables and set _isMovingDown to false, so we no longer move down and those X and Y coordinates are finalized.

Still in the Update, when _isMovingDown is false we will call theCircularMovement method.

Creating this circular movement was a bit difficult because it took me back to math class. So we first need to create a int and float variable that will store the radius (_orbitDistance) and the angle the object is current at.

In the CircularMovement method, we Update the current angle by 0.01 (the more you increase the faster the enemy will move).

We created a local float value that will contain the new X position as it is updated and we will set it equal to the enemy’s X position when it stop plus the COS of the current angle x the orbit distance.

What the COS does it has the X value bounce back and forth between -1 and 1. The center of the circle is the current X Position. The angle is what we are increasing every frame and the hypotenuses of the triangle (the radius of the circle) is our orbitDistance so the blue line is show you what is being added to the current X position. Every time we change the angle we are either adding or subtracting between 10 and -10 (orbitDistance).

We are basically doing the same thing with the Y position, but instead of using COS, we will use SIN.

Similar idea with the Y bouncing back and forth between -1 and 1 as the angle increases. The difference is when the angle is 0 the SIN of Y= 0 and when it gets to 90 degrees the SIN of Y= 1. For the COS of X at 0 degrees is equal to 1 and when the angle reaches 90, the COS of X is equal to 0.

The Z will remain the same since we are dealing in 2D and with the new X and Y coordinate we will pass in these local variable into a new Vector3. Since this is Update and we are increasing the current angle every frame, this position is constantly being change. With the combination of COS of X and SIN of Y creates this circulate movement in our enemy.