PART TWO: SMOOTH MOVEMENT & RANDOM LOOKING
Using Unity Version 2019.4
If you've been following along with the first part of this tutorial series, you should have a basic eye movement and targeting system in place. If not, check it out and meet us back here!
Let's recap what we should have in place:
Our character is set up with an eye movement monobehaviour, which uses either eye bone transforms or UV texture offsets.
Your character has an eye rig parented to the head transform.
Our character has an animator controller that contains a Default state, with no behaviour, and a LookAtTarget state which uses a LookBehaviour script.
There are two things we want to do to our eye movement system at this point: 1) Smooth out the transitions to and from a look state, and 2) Extend our system to allow our character to randomly look around.
The approach will differ greatly depending on the method you've chosen, but both will make use of coroutines.
The Unity Manual does a pretty good job of explaining coroutines:
"[A coroutine] can be used as a way to spread an effect over a period of time...it is also a useful optimization. Many tasks in a game need to be carried out periodically and the most obvious way to do this is to include them in the Update function. However, this function will typically be called many times per second. When a task doesn’t need to be repeated quite so frequently, you can put it in a coroutine to get an update regularly but not every single frame."
Think of a coroutine like a "to do" list. Rather than execute all of your commands in a single frame update like a normal function, a coroutine allows you to go through your list of commands over time, repeat them on a loop until a condition is met, or even wait before executing a subsequent command.
So with that in mind, let's get to the tutorial part of this tutorial.