Today we will see what the deal is
with all this IK stuff.
We have done some work with IK before,
but we will see all of it in practice.
Let's start first with the foot IK,
since we already did some hand IK and some look IK.
The first thing I'm going to do is,
I have a character over here.
And I'm going to create a new script.
So that we have an IK, to write an IK code set.
So I'm going to name this "IK Handling."
And I'm going to drop it into my character.
Now for the time being,
I don't want the scripts,
but since I'm taking the avatar from beneath my character,
I'm going to leave them right now and use them later.
Now, one thing we need to do first,
is our layers. Now I have these layers I don't really need right now.
If we want to work on a layer, on a meta [?] layer with IK,
we have to have this "IK Pass" [setting] ticked,
and if you have an avatar mask on a layer that has no other mask,
you also need to have, to [not be cooling?] the IK positions. OK.
This needs to be up.
Now for the base layer,
the base layer automatically takes
all the bones of the animation.
Now we open up my IK handle script,
First of all we need to know the animator.
And we do it like this, as always.
Let's start first with some targets.
And I'm going to give it:
"public Transform leftIKTarget;"
"public Transform rightIKTarget;"
And I also want to control the weight,
so I'm going to also add a public float of...
I'm going to name it "IKWeight"
and I'm going to use the same float for both of the targets.
Everything that has to do with IK must go inside on animator IK.
The first thing we're going to do is set the IK position.
Wait! Set the avatar IK goal, I'm going to say left foot, and the float value
will be the IK weight we just created.
I'm going to copy paste this,
and I'm going to change this to 'right foot',
and I'm going to copy paste all of them and instead of set IK position weight,
I'm going to say set IK position.
And the avatar IK goal will be the left foot,
but over here we need to "pos" a vector 3, which in our case will be
left IK target position, for now.
So I'm going to copy paste ... and for the right foot,
I'm going to add IK position.
Let's initialize this to 1.
And now back into here [?]
I'm going to drop my IK target left, and IK target right.
I'm going to hit play
and I'm going to close the scripts for now,
and you will see that... the left foot is trying to reach the IK target
and as you see over here, it will go wherever we set it to go.
Wherever we place our IK target.
Now, when something like this happens,
the knee is all wrong.
So, how we can fix that? We can say, [inaudible]
to new transforms, and the same as we did the avatar IK goals,
we can say set IK hint position, weight,
and set IK hint position.
So. Now of course this gives us an error,
because... we have the avatar IK goal.
But a hint is not an IK goal, it is an avatar IK hint,
and this doesn't have a left foot, but you can see it has a left elbow, left knee,
right elbow, and right knee.
So it's the same for hands and feet, it's actually the same.
The same way you do it.
So, we will use the left knee, we will copy-paste this over here,
and over here,
and we will say, this... we want this... right knee...
and instead of the target position, we are going to use the hint positions.
So, now we need to drop the hint positions,
I have already made those,
hint left, hint right, and now when we hit play, you will see that
even if I move my legs over into a weird position,
if I move the hints around, it will actually make the knee rotate
to fix his position.
So, with this you can fix a lot, [or] some, weird issues. OK.
But, as you can see, wherever I leave my IK target,
it will always have the same rotation.
So. How I can fix the rotation is simply by doing, instead of anim [...]
set IK position weight, I can do set IK rotation weight.
And then set IK rotation.
And of course, this won't take that position, but it will take the rotation.
Let's go back over here.
And now we will see that our IK target
will always have the rotation of...
The left foot will always have the rotation of the IK target.
Now. This will have to automate all this stuff,
and we want to automate it so that when we walk,
we don't have to minorly move these around.
So. Let's see how we can do that.
First off, what we need to do is of course know when we don't want our animator
to move, to use the IK.
So. We know that if we are walking... On a walk,
as you see, when he raises his feet from the ground,
we don't want to use IK, we want to play the animation.
But when he puts his feet down to the ground,
then we want to enable the IK.
And the way we do this, the same way we've already done this,
is, we'll create two new floats inside our animator.
I'm going to name one left foot and the other right foot.
I've already made these.
And then we will go into our animation,
and we will go into 'curves', and ... let me see how we can, uh...
And you will see that for the left foot,
the value for the left foot for the curve
is at 1 when we hit the ground.
But as we are moving, and
as he has to raise his feet off the ground,
then the moment his foot is not touching the ground,
the curve is at 0. And it rises again when he has to hit again.
So we can even go a lot more.
This is a little bit exaggerated
so that you can see what we are doing.
So I'm going to give him even more air
before he's touching the ground,
and I'm also going to add some more 0,
I'm going to give him more time with the IK closed.
And I'm going to make it only for the left foot,
so that we can decide ...
so that we can see the difference [between the left and right foot]
between giving him more air on the curve to have the IK enabled,
and the IK disabled.
Now in my animations over here, I have only added - of course hit 'apply' -
I have only added curves into my idle state,
so in this idle state, this curve over here, if you open it,
if you click the first one,
we will make the curve for all of his length [legs?] have the 1 value.
OK, so that means when we are idle, when we are in the idle state,
our left foot and right foot [inaudible] will always be at 1,
so that means we have enabled Foot IK.
Keep in mind, how you set up the curves
play a big role in how it's going to sole [?] the IK.
OK, so let's back into our script
and let's start doing some things.
Now, I don't want to use any more the left IK and right IK targets.
Since we're going to be deciding those positions,
we always script.
But I'm going to leave them since we're going to use them probably,
even though the hand IK is pretty much the same for the hand,
if I change here, left hand, for instance,
Right foot... left... this will be the same,
um, no, this we will change, and here say, left elbow
and we can position.. we need to "pos" the left elbow,
and the rotation of course... left hand, and left hand.
So now, instead of moving the left foot, it will change the left hand
and it will move him around.
OK. It's pretty much the same with the legs.
But we're going to see some more examples
that will save us work, in our workflows.
OK. It's pretty much the same.
So, let's [sample?] our script.
For now, I'm going to change this back to where it was.
Not left hand, left foot.
I'm going to delete the hints, or maybe just 'comment' them out.
And I'm going to leave left foot...
(correcting code from 'feet' to 'foot') He doesn't have a lot of feet,
he only has one.
I'm going to also add these back to the way it was.
So we have all this... let me bring it over here.
And now let's add some variables.
We want a vector 3 for the left foot position,
and we also want a vector 3 for the right foot position.
We also need two Quaternions.
One for the left foot rotation,
and then a Quaternion for the right foot rotation.
OK. So in our IK goals, let's [hands?] all this.
We need two floats. The first one will be left foot weight,
and the other will be right foot weight.
So, let's change our weight for both the position and rotation,
to the appropriate ones.
So with these two separate floats, it will change the weights
independently of one another.
So now that we have our floats, we need to set them ... to connect them
with the parameters from inside the animator,
where we set our curve and all the stuff.
And the way we do that is by simply using "anim.GetFloat",
and we have the left foot,
and for the right foot weight, we want to take ... the right foot.
OK. So this opens up - manipulates the weight, with how the animator works, and
what animation plays, and what group we have
on the animation we're currently playing.
Now, we want to change the targets,
instead of the IK target, I'm going to "pos" the left position,
and I'm going to pos the right position to the right foot.
And for the rotation, I'm going to pos
the new Quaternions I made for the new rotation.
So, now that we have set this up, we need a way to say... how to know where to place
our foot, and what rotation it will have.
So I'm going to make a new Transform,
which I'm going to name left foot,
and the one for the right foot, I'm going to name right foot.
And then, for inside our Update,
we will create two Raycasts that make a raycast down to the ground
and see what are we hitting.
So we need a "RaycastHit" variable,
which we're going to say "leftHit", and we need a right one too, "rightHit".
We need to change, to Transform from local position to warped position,
the position of a left and right foot,
so we'll create a Vector 3, and we will call this
"lpos," standing for "left position".
And then we will say "leftFoot.Transform,"
point Vector 3 dot zero.
And this will transform the local position to world position.
And for the right foot we create "rpos" for "right position,"
and then we will take the right foot.
So, now we have our positions, we need to do our own raycasts.
We know for the left position will be left position,
we know we want to do the Raycast downwards,
we want to save it in left hit,
and the distance... we can play a lot
with the distance, but I'm going to use 1.
And also, even though we are doing
a raycast from the ground, you have to keep in mind that
we might also hit our own collider.
So, when we do the raycast toward the ground,
and if we hit something, that means we have a left foot position.
So we do a left hit dot point. "leftHit.point"
And then we have a position for [our K gone ?]
We need the rotation, and that will be "leftFoot.rotation"
But we can't just do "leftHit.normal" because
this is a vector 3.
So we need a way to transform that into a Quaternion.
You can't do a "Quaternion.euler" also,
because it won't have the results you want.
So what we need to do is take our Quaternion and make it from pass the transfer.
The top, with our left hit, dot normal,
and we multiply this by transform dot rotation.
Now, if you don't understand this, don't worry.
Quaternions are a little bit difficult to understand.
So we need the same for our right foot, OK.
Pos the right position, pos the right hit,
and we want the right F position, and the right F rotation,
and instead of the left hit, we need the right hit, dot normal.
And just like that, if we go back...
No, before we go back.
We haven't set this up,
so if we hit play now it will give us a null reference error.
And that is because we haven't set this up
so the easy way to do that is,
since we know we have a humanoid,
we can say get bone transform, and
call it "human body bones" in animator,
and say we want the left foot.
And it will be the same for the right foot,
and we have set up our transforms.
So.
Let's save, let's go back.
And if I hit play,
we will see that he's actually lost his legs,
and we are probably getting errors.
Now, let's see what we forgot.
There are two problems over here.
One is the Q-none, and this is because
one of our Quaternions is returning "000".
So, the Q-none error is about Quaternions.
Let's open up our first left foot.
This one will get the error because
we can't find the left foot over here.
That means something is wrong.
Let's click the pos, and let's hit play again.
We get the error once again,
but we take it on the other foot.
Let's see where the error is. What did we forget?
It's probably because ... it hasn't already set up the animator,
and there is no avatar.
So I'm going to use ... to drop, minorly, the avatar.
And put it over here,
and let's see if we get the errors right, now.
We still get an error.
Let's 'comment' out our rotation.
Since it's a problem with our rotation.
Let's comment this out. Saved.
Let's go back.
And, now we see that we have our character
is moving our feet and he's sticking to the ground.
When he's not moving, we can see that his rotation, the IK will always work.
In our characters crib over here,
we are also using physics,
and we do changes from inside the scripts,
so I'm going to close this.
And let's see over here.
You see that he sticks to the ground,
because we're doing the raycast towards the floor. [IK/OK]
Let's see where the error for the Quaternions were.
The problem with our Quaternion was
even though we asked for our Quaternions
to go from one rotation to the other,
but they were null, meaning that
we didn't have any rotation upon those.
And the way to fix that is simply, we are taking the left foot ...
and the left foot rotation will be, the left foot rotation,
and for the right foot, will also be the right foot rotation.
So let's go back, let's hit play,
and for now I'm going to close
the user input and the character movement.
And I'm going to take a small cube I have over here
and bring it over here,
and I'm going to put it underneath our character.
And you can see that it moves
But we can't see his feet,
so let's add a little offset.
Let's make it public and
we're going to sat, read the public.
Let's say offset Y.
And then when we set our position,
we will say, we take the left position,
plus a new vector 3," 0,offsetY,0".
Then once again,
Let's go into our third person character
and set the offsetY to something like, point 12 (0.12)
and let's hit play.
Let's bring our cube once again.
And as you have already seen, he moves.
He's now going up, because we have a collider over here.
Let's make this a trigger
so that he's still moving,
because we are making a raycast also
from our character movement script.
So I'm going to close the user input ...
and... that was probably a bad idea...
Let's try this again.
Let's bring this over here.
Let's hit play.
And then I'm going to make him a kinematic
and I'm going to say he's a trigger.
And I also want to close the user input
and the character movement script for now.
When I bring my cube from beneath,
you will see that he's not only placing his feet,
he's also rotating them along the correct position.
Keep in mind if it's something like this
you will probably need to play a different animation.
IK is not for animating,
it's not going to save you from animating stuff.
Although it's pretty capable.
And the way you use it could be pretty capable.
You will also need animations
to further prove the IK.
So IK is mostly for correctional use
and not for, as something you use
independently from animations.
So, now that we see how it works when it's stationary,
let's see how it works when we are moving around.
I'm going to focus my view on his feet,
so that we know what is happening.
Something like that.
And I'll hit play and I'll move around.
Keep in mind, when we are running
we don't have any floats,
we don't manipulate the floats of our curves,
of our animator.
So, he doesn't influence the IK,
but if I hit shift when we are walking,
you will see his left foot walks a little better
since we fixed that.
We have more control, we made it
not to be influenced so much by the IK.
And the right foot we left (alone)
so that we can see the difference.
And now if I'm going up the ramp,
you will see that he positions his feet
to the ramp.
This is a lot influenced by the IK.
If you play with the values,
and put them exactly as you want,
you'll get much better results
than from what we already have over here.
Typically in games mostly, they are
not so dependent on the IK, and
a lot of the time they just change the animation
and just correctly place the feet
so that doesn't look ugly.
Mostly when the character is sitting idle
you'll see that they change his position.
They enable the IK for him.
Let's see how he will walk over here.
Now, even though we have the IK open
we also have the collider over here
which doesn't let him pass over other objects.
So if I close it, you will see that
he's moving and he places his feet.
If you spend more time setting up the floats
and where exactly you want them,
you'll have better results.
Sometimes you'll see that the feet might
even get some rotations that are
not really natural, so you can fix
how much of a rotation you want the feet
to be able to do.
If you go.. before applying the rotation
over here, you can say, take the left foot rotations,
let's take the Y and make it 0.
This is just an example to show you
how you can do it.
It's not useful to have without testing first.
Here you will see that whatever rotation
we're having on the Y, he will return,
he will always be 0.
The rotation will always be 0.