Discover how to create catchy animations for your Unity 3D objects with a few lines of C#!
This article is also available on Medium.
Ever since we began making games, they have been filled with collectibles to reward and guide the players. Be it coins, gems or feathers, creators have had fun disguise them in many ways… but oftentimes, one constant remained: they’ve always had some special VFX or cute animation to stand out and get our attention.
Such animations can be complex, or dead simple… but usually, they actually take no more than one or two lines of code in modern game engines!
So, today, let’s see a few short C# snippets to create handy animations: rotation, periodic movement or waypoint-based translation 🙂
Making an object rotate continuously
Something that is quite common in video games is to have some objects float in the air and rotate continuously along some axis; this is a good way to catch the player’s eye, and this technique was already used in old 2D games, with just a series of sprites to simulate this movement:
But how can you do the same in 3D? Since we don’t have sprites, we’ll need to update the transform of our 3D object, instead. This can be done very easily with just one line of code:
And here you are: you have a 3D floating coin! 🙂
You can even expose the rotation axis in the Inspector to let the user select how the objects rotates:
Having an object hover periodically
Another cool effect to grab people’s attention is making your object move up and down, as if it was powered by some magic:
Again, this is quite straight-forward to do, thanks to trigonometric functions (like a cosinus or a sinus). Those functions are what we call “periodic functions”: their value cycles through a given set of values:
That’s why they are an easy way to make a regular movement: even if we constantly increase the current game time, computing
sin(time) will always give us a value between
So we can simply set the vertical position of the object based on this trigonometric value, and multiply it by a given
amplitude to make the object move to further extrema:
If you want the object to move slower or faster, the trick is to multiply the
time (inside the trigonometric function) by a factor
f – use
f > 1 to speed the object up, and
f < 1 to slow it down:
Moving an object between several waypoints
What if you have some enemy you want to put on patrol? Or a platform you want to move around following a specific path marked with waypoints?
This is a good use for the
Vector.MoveTowards() function that automatically computes the right amount of movement to get a current position to a target position.
So let’s say you placed a few empty objects in your scene as waypoints (you can give them visible icons by clicking on the little cube image in the top-left corner of the Inspector):
Then, you can write a simple C# script to assign to your “patrolling” object to reference and use those waypoints (don’t forget to set the
waypoints array in the Inspector):
In this short article, we saw a few handy tips for creating catchy animations for Unity 3D objects with C#, like rotation, periodic translation, or waypoint-based movement.
I hope you like this quick tutorial – and of course, don’t hesitate to post a comment to share other ideas of Unity tips and programming articles! Thanks a lot for reading 🙂