Return to Video

Unity 5 Tutorial The Build-In IK System

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

more » « less
Video Language:
English
Team:
Captions Requested
Duration:
01:16:58

English subtitles

Incomplete

Revisions