Search Unity

Problem while using inverse kinematics

Discussion in 'Animation' started by Deleted User, Mar 18, 2018.

  1. Deleted User

    Deleted User

    Guest

    Hey all,

    Currently I'm using inverse kinematics to let my thirdperson player models hold their properly (using a transform inside of the thirdperson weapon object). The only problem is, when my character aims up or down to possible of the hand is not where it is suppossed to be (the aiming animations are on a seperate layer in the animator).

    The code I'm using for IK:
    Code (CSharp):
    1. void OnAnimatorIK(int layerIndex)
    2.     {
    3.         if (WeaponIK) {
    4.             PlayerThirdPersonAnimator.SetIKPositionWeight (AvatarIKGoal.LeftHand, 1f);
    5.             PlayerThirdPersonAnimator.SetIKRotationWeight (AvatarIKGoal.LeftHand, 1f);
    6.             PlayerThirdPersonAnimator.SetIKPosition (AvatarIKGoal.LeftHand, ThirdPersonWorldWeapon.GetComponent<ThirdPersonWeapon> ().LeftHandTransform.position);
    7.             PlayerThirdPersonAnimator.SetIKRotation (AvatarIKGoal.LeftHand, ThirdPersonWorldWeapon.GetComponent<ThirdPersonWeapon> ().LeftHandTransform.rotation);
    8.             if (ThirdPersonWorldWeapon.GetComponent<ThirdPersonWeapon> ().RightHandTransform != null) {
    9.                 PlayerThirdPersonAnimator.SetIKPositionWeight (AvatarIKGoal.RightHand, 1f);
    10.                 PlayerThirdPersonAnimator.SetIKRotationWeight (AvatarIKGoal.RightHand, 1f);
    11.                 PlayerThirdPersonAnimator.SetIKPosition (AvatarIKGoal.RightHand, ThirdPersonWorldWeapon.GetComponent<ThirdPersonWeapon> ().RightHandTransform.position);
    12.                 PlayerThirdPersonAnimator.SetIKRotation (AvatarIKGoal.RightHand, ThirdPersonWorldWeapon.GetComponent<ThirdPersonWeapon> ().RightHandTransform.rotation);
    13.             }
    14.         } else {
    15.             PlayerThirdPersonAnimator.SetIKPositionWeight (AvatarIKGoal.LeftHand, 0f);
    16.             PlayerThirdPersonAnimator.SetIKRotationWeight (AvatarIKGoal.LeftHand, 0f);
    17.             PlayerThirdPersonAnimator.SetIKPositionWeight (AvatarIKGoal.RightHand, 0f);
    18.             PlayerThirdPersonAnimator.SetIKRotationWeight (AvatarIKGoal.RightHand, 0f);
    19.         }
    20.     }
    I hope someone can help me dealing with this problem.

    -Pablo Discobar

    PS: Attached are some images to visualise my problem a bit more.
     

    Attached Files:

  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Hey Pablo - is your IK ordered to perform in Late Update?
     
  3. Deleted User

    Deleted User

    Guest

    I'm not sure what you mean by that, for IK I only ticked the ikpass on the animator for the appropiate layer and handled everything so far in the OnAnimatorIK function.
    The only thing I'm running in the lateupdate is setting the aimangle animation of my player like this:

    Code (CSharp):
    1. public void HandleThirdPersonAiming()
    2.     {
    3.         if (ThirdPersonPhotonView.isMine) {    //If this is our instance of the game
    4.             float AimAngle = PlayerCamera.transform.localRotation.x;    //Get get our current local aimangle
    5.             PlayerThirdPersonAnimator.SetFloat ("AimAngle", AimAngle * 2f); //Set the float AimAngle in the animator on our player instance
    6.         }
    7.         if (!ThirdPersonPhotonView.isMine) { //If this is a other player
    8.             PlayerThirdPersonAnimator.SetFloat ("AimAngle", Mathf.Lerp (PlayerThirdPersonAnimator.GetFloat ("AimAngle"), SyncedAimangle, 0.05f)); //Lerp the aimangle so it looks smooth on our instance
    9.         }
    10.     }
    -Pablo Discobar
     
  4. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
  5. Deleted User

    Deleted User

    Guest

    Or maybe the rig / model I'm using is just a bit off regarding IK because it's converted from a generic rig. So I contacted a animator to modify the animation, so it looks properly without the use of IK. Anyhow, still thanks for trying to help me out with this problem!
    -Pablo Discobar
     
    theANMATOR2b likes this.
  6. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Sure thing Pablo. Glad you solved. I look forward to seeing more of your project in the future. :)
     
  7. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    @Pablo_Discobar I don't know if you did fix your issue, but those kind of issue are very often related to the fact that your IK pass is done on a layer before aiming layer.

    So from what I understand you did all your aiming on an another layer, and if we look at your OnAnimatorIK(int layerIndex) method we can clearly see that the IK is done no matter which layer is invoking the method. There is a layerIndex parameter that allow you to do different IK pass depending on the layer.
    So let's say that you're aiming occur on layer 2, when this layer have finish to be evaluate you know that the upper body position should'nt move anymore so you need to do your IK pass either on layer 2 or after. So first be sure to tick ik pass only on layer after the aiming layer and also add an if(layerIndex == ??) in you're OnAnimatorIK method.
     
    theANMATOR2b likes this.