By using inverse kinematics ik, we can control the role of a particular part of the body to rotate or move as needed to achieve some of the desired effects, such as: when moving, let one foot with injury drag; Let your hands lift up to get the apples on the table, and keep your head facing our color camera as if it were watching us all the time.
Let's start with a simulated wounded foot:
Before doing this, check the Ikpass property in the character's animation state machine, applying IK's layer:
Then, under the character object, create an empty sub-object iktarget, and place it in the right ankle.
Finally, add the script to the character Ikcontroller:
1 usingUnityengine;2 usingSystem.Collections;3 4 Public classIkcontroller:monobehaviour5 {6 PublicTransform Target;//ik target7 Public BOOLikactive;//whether IK is available8 PrivateAnimator Anim;9 Ten voidAwake () One { AAnim = Getcomponent <Animator> (); - } - the //ik control to write in the Onanimatorik Method! - voidOnanimatorik () - { - if(ikactive) + { - //Specifies the IK weights of the body parts to be controlled, 1 is enabled, and 0 is not enabled. +Anim. Setikpositionweight (Avatarikgoal.rightfoot,1); AAnim. Setikrotationweight (Avatarikgoal.rightfoot,1); at - //set the target value for IK - Anim. Setikposition (Avatarikgoal.rightfoot, target.position); - Anim. Setikrotation (Avatarikgoal.rightfoot, target.rotation); - } - } in}
Set Target to our just created empty object Iktarget, run the game, let the character move animation play up, you can see the right foot of the animation is not playing, but dragged away. We can change the position and rotation of the iktarget in the scene view, the right foot of the protagonist will also move, of course, this can also be controlled by code.
One more control character has been staring at the color head:
Put the color camera in front of the character and add a bit of code to the script above:
1 anim. Setlookatweight (1); 2 // look here , eyes . 3 Anim. Setlookatposition (Gameobject.findwithtag ("maincamera"). transform.position);
OK, run the game, move the color camera, don't keep looking at people, ah, will be shy ^^!
Animatorcontroller inverse kinematics ik