UnityCreate a Constant Horizontal Movement in Unity

Rusben Guzman
Sep 8, 2021

Objective: Use the Mathf.Sin() function to make a constant horizontal movement for the enemy boss

First I declared some variables. _startPosition is the central point of the movement. The frequency and the magnitude to adjust the range of movement of the enemy when we use the formula.

Then in the start function I initialized the _startPosition variable and called the entrance animation coroutine.

The last step was to create a coroutine to make the movement. I assign the start position plus ths cosine of the current time to the position of the boss and wait for the next frame.

And that’s it!!! We have the constant horizontal movement ready.

--

--

Rusben Guzman

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.