Reply To: Transferring mechanical rigs from blender to unity

Forum Archive Transferring mechanical rigs from blender to unity Reply To: Transferring mechanical rigs from blender to unity

#33224
Jonathan Gonzalez
Participant

    Looking through the streams I noticed I did use IK but I didn’t really cover it much outside of setting up a few Animation layers and enabling IK but I can provide the script I use often for humanoid IK:

    using UnityEngine;
    using System.Collections;

    //add IK to the first layer in the Animator controller and add a second layer with IK enabled to control the arms

    public class AimIK : MonoBehaviour
    {
    [Header (“IK Transform References”)]

    public Transform ikReference = null;
    public Transform leftHandle = null;
    public Transform rightHandle = null;

    private Vector3 target;
    private Animator animator;

    [Header(“IK Weight Values”)]
    [Range(0,1)]
    public float aimWeight;
    public float aimAdjustment, bodyWeight, headWeight, eyesWeight, clampWeight;

    // Use this for initialization
    void Start ()
    {
    animator = GetComponent<Animator> ();

    }

    void OnAnimatorIK ()
    {
            target = ikReference.position;
    target.y = target.y + aimAdjustment;
    animator.SetLookAtPosition (target);
    animator.SetLookAtWeight (aimWeight, bodyWeight, headWeight, eyesWeight, clampWeight);

    if (leftHandle != null) {
    animator.SetIKPosition (AvatarIKGoal.LeftHand, leftHandle.transform.position);
    animator.SetIKRotation (AvatarIKGoal.LeftHand, leftHandle.transform.rotation);
    animator.SetIKPositionWeight (AvatarIKGoal.LeftHand, aimWeight);
    animator.SetIKRotationWeight (AvatarIKGoal.LeftHand, aimWeight);
    }

    if (rightHandle != null) {
    animator.SetIKPosition (AvatarIKGoal.RightHand, rightHandle.transform.position);
    animator.SetIKRotation (AvatarIKGoal.RightHand, rightHandle.transform.rotation);
    animator.SetIKPositionWeight (AvatarIKGoal.RightHand, aimWeight);
    animator.SetIKRotationWeight (AvatarIKGoal.RightHand, aimWeight);
    }
    }

    }

    This was used for this robot character: Robot Controller IK. It’s the same script I use when I need generic IK for humanoid characters. It allows you to use a target for the character to follow and has a few sliders for adjusting the view so if it’s too high or too low you can tweak it. It also has transforms to be used as handles on the hands. This will allow you to use IK on the hands to move the arms around and place them into a different pose. 

    In order to use this make sure you apply this script to the object that has the Animator component. In the animator controller you may need to create a second layer and enabled IK, also set the layer weight to 1. This may not be needed anymore, but before this was the process of getting IK to work. 

    Then just assign transforms for the target (ikReference) which will be used to have the player look in a specific direction. The handles are for the hands, so place these near the hands. You can then play the scene and make adjustments to the IK as needed. It’ll require going back and forth a lot. When you’ve made changes to one of the handles or target you can copy the transform component then get out of player mode and paste those values in.