The way we have our current character set up is to use the LookAt to turn towards the target location
but the problem with this is it makes your character snap instantly around.
Instead we want to introduce a method so that our character will turn slowly towards the target.
To do that, we're going to use Spherical Linear intERPolation or SLERP for short
and what it does is to interpolate between the vector we're currently facing and the direction that
we want to be facing in.
It does this over a particular time period, so that you can turn by a little angle each time until you
reach your goal direction.
Let's start implementing this in Unity. So we're going to modify the previous project that we ended with
inside of the MoveLocal script.
The first thing we need is a float for the rotation speed, so let's call it rotSpeed and I'll set that to 0.5f.
Chances are once you start moving your characters around, you won't want the speed and the rotational
speed to be exactly the same.
So having two different values works really well because you can adjust either one. Of course, in this case,
I DO have them the same.
In fact, I might just turn that down to 0.1.
Now, we have a lookAtGoal, but we also need to calculate the direction that we want to be facing
so we're going back to what we had originally and combining these two.
So we want to work at our direction that we need to face.
So it's a Vector3 direction and it's going to be equal to our lookAtGoal minus our current position.
Now the only other change we need to do is to turn this LookAt here into a slerp that will use the direction
as its bearing.
So delete the LookAt line and replace it with this.
The slerp is setting the rotation of your character, so therefore we're doing a this.transform.rotation =
rather than a position or a Translate and this .rotation is then set using the slerp method
that you can find in the Quaternion class. Quaternion is a complex data structure that's holding a rotation
and it can do all sorts of fantastic things
but in this case it's turning an angle from facing in one direction to another.
So the slerp itself takes the current rotation of your character.
It then performs a LookRotation on your direction, so direction is our vector we calculated which is pointing
towards our goal, but we can't do the slerp on the direction vector.
We want to do the slerp on the angle that we have to rotate through, to get from where we're currently
looking to the particular direction.
So this Quaternion.LookRotation will
give us the rotation that needs to occur to be facing in 'direction' and thereby combining these two and
comparing them little-by-little,
then we slowly turn from one to the other.
Now the rate at which we turn is the third value that you put inside of this method and we're basing
it on the same type of speeds that we use for moving a character forward, where we've got deltaTime
multiplied by our rotational time.
Right.
So let's save that and go into Unity and have a look at how this operates. Make sure your character
is facing away from the goal, so that you can see the turning occurring. So press Play and the character
will still be getting pushed forward along its forward vector, which is what we want
and at the same time, that slerp is operating to turn your character around and you can see here in the
Scene view that it's starting to arc around towards there.
Now it's a little bit slow, this rotational speed, so let's just stop that.
Go back into our
MoveLocal and increase it.
So, if we put that up to say 4 and save it and then run that again.
We find that the character turns much more sharply and we can now drive our cube around and the character
will turn and follow it.
So that's how you move your character forward and how you perform a slerp to get your character facing
in the right direction
and just in these few videos, we have covered pretty much all the essential basics that you're going
to need to get some very complex behavior happening in NPCs.