Easily Create Complex Movements for GameObjects in Unity using Animation Curves

Published

This is a nice trick if you want an object to move in a smooth, but seemingly random way, around a point of interest, while allowing easy customization.

You can define a maximum distance for this movement, and define a position on each axis at which the object should be when it reaches a given point in its movement cycle by acting on the shape of the Animation curves.

The next step is then to define the duration of a “cycle”, and use it in a periodic function along with the elapsed time to know a which point of the cycle we are. The formula is the following: sin(2 pi elapsed / cycleDuration). Here we use 2 * pi to handle frequency in seconds (the angular frequency of the sine function).

Finally, we evaluate the Animation Curve and modify the local position of our object accordingly.