Search Unity

Question How to make the player finger on the right hand to point a target ? using IK

Discussion in 'Animation' started by shamenraze1988, Jun 18, 2021.

  1. shamenraze1988

    shamenraze1988

    Joined:
    Nov 24, 2020
    Posts:
    208
    For now, I can make the hand-raising up pointing a target but it looks a bit weird.

    Code (csharp):
    1.  
    2. private void InteractWithTarget(InteractableItem primaryTarget, float closestLookWeight)
    3.     {
    4.         if (primaryTarget != null)
    5.         {
    6.             if ((lastPrimaryTarget != null) && (lastPrimaryTarget != primaryTarget) && (finalLookWeight > 0f))
    7.             {
    8.                 // Here we start a new transition because the player looks already to a target but
    9.                 // we have found another target the player should look at
    10.                 transitionToNextTarget = true;
    11.             }
    12.         }
    13.  
    14.         // The player is in a neutral look position but has found a new target
    15.         if ((primaryTarget != null) && !transitionToNextTarget)
    16.         {
    17.             if (primaryTarget.IsAnyAction())//.interactableMode == InteractableItem.InteractableMode.ActionWithoutThrow)
    18.             {
    19.                 RightHandToTarget = true;
    20.             }
    21.  
    22.             lastPrimaryTarget = primaryTarget;
    23.             //finalLookWeight = Mathf.Lerp(finalLookWeight, closestLookWeight, Time.deltaTime * weightDamping);
    24.             finalLookWeight = Mathf.Lerp(finalLookWeight, 1f, Time.deltaTime * weightDamping);
    25.             float bodyWeight = finalLookWeight * .1f;
    26.             animator.SetLookAtWeight(finalLookWeight, bodyWeight, 1f);
    27.             animator.SetLookAtPosition(primaryTarget.transform.position);
    28.  
    29.             if (RightHandToTarget && primaryTarget.IsAnyAction())
    30.             {
    31.                 Vector3 relativePos = primaryTarget.transform.position - transform.position;
    32.                 Quaternion rotationtoTarget = Quaternion.LookRotation(relativePos, Vector3.up);
    33.  
    34.                 if (primaryTarget.interactableMode == InteractableItem.InteractableMode.ActionWithoutThrow)
    35.                 {
    36.                     animator.SetIKRotationWeight(AvatarIKGoal.RightHand, finalLookWeight);
    37.                     animator.SetIKRotation(AvatarIKGoal.RightHand, rotationtoTarget);
    38.                     animator.SetIKPositionWeight(AvatarIKGoal.RightHand, finalLookWeight * 1f * closestLookWeight);
    39.                     animator.SetIKPosition(AvatarIKGoal.RightHand, primaryTarget.transform.position);
    40.                    
    41.                 }
    42.  
    43.                 if (primaryTarget.interactableMode == InteractableItem.InteractableMode.Action)
    44.                 {
    45.                     animator.SetIKRotationWeight(AvatarIKGoal.RightHand, finalLookWeight);
    46.                     animator.SetIKRotation(AvatarIKGoal.RightHand, rotationtoTarget);
    47.                     animator.SetIKPositionWeight(AvatarIKGoal.RightHand, finalLookWeight * 0.1f * closestLookWeight);
    48.                     animator.SetIKPosition(AvatarIKGoal.RightHand, primaryTarget.transform.position);
    49.                 }
    50.  
    51.                 // -> new code block
    52.                 if (finalLookWeight > 0.95f) // here you can play with a value between 0.95f -> 1.0f
    53.                 {
    54.                     if (primaryTarget.interactableMode == InteractableItem.InteractableMode.Action
    55.                         && hasSent == false)
    56.                     {
    57.                         target = primaryTarget;
    58.                         toTarget = true;
    59.                         hasSent = true;
    60.                         primaryTarget.description = "";
    61.                         // Here I need to find where to make hasSent false again.
    62.                     }
    63.                 }
    64.                 else
    65.                 {
    66.                     hasSent = false;
    67.                 }
    68.  
    69.                 if (finalLookWeight > 0.9f)
    70.                 {
    71.                     handFinishedMove = true;
    72.                 }
    73.             }
    74.         }
    75.  
    76.         // Let the player smoothly look away from the last target to the neutral look position
    77.         if ((primaryTarget == null && lastPrimaryTarget != null) || transitionToNextTarget)
    78.         {
    79.             finalLookWeight = Mathf.Lerp(finalLookWeight, 0f, Time.deltaTime * weightDamping);
    80.             float bodyWeight = finalLookWeight * .75f;
    81.             animator.SetLookAtWeight(finalLookWeight, bodyWeight, 1f);
    82.             animator.SetLookAtPosition(lastPrimaryTarget.transform.position);
    83.  
    84.             if (RightHandToTarget)
    85.             {
    86.                 Vector3 relativePos = lastPrimaryTarget.transform.position - transform.position;
    87.                 Quaternion rotationtoTarget = Quaternion.LookRotation(relativePos, Vector3.up);
    88.                 animator.SetIKRotationWeight(AvatarIKGoal.RightHand, finalLookWeight);
    89.                 animator.SetIKRotation(AvatarIKGoal.RightHand, rotationtoTarget);
    90.                 animator.SetIKPositionWeight(AvatarIKGoal.RightHand, finalLookWeight * 0.5f * closestLookWeight);
    91.                 animator.SetIKPosition(AvatarIKGoal.RightHand, lastPrimaryTarget.transform.position);
    92.             }
    93.  
    94.             if (finalLookWeight < lerpEndDistance)
    95.             {
    96.                 transitionToNextTarget = false;
    97.                 finalLookWeight = 0f;
    98.                 lastPrimaryTarget = null;
    99.                 transform.rotation = Quaternion.Euler(0, transform.eulerAngles.y, 0);
    100.             }
    101.         }
    102.  
    103.         // Show primary object found by the player
    104.         if (primaryTarget != null)
    105.         {
    106.             if (primaryTarget.description != "")
    107.             {
    108.                 descriptionTextImage.SetActive(true);
    109.                 text.text = primaryTarget.description;
    110.             }
    111.         }
    112.         else
    113.         {
    114.             text.text = "";
    115.             descriptionTextImage.SetActive(false);
    116.         }
    117.     }
    118.  
    The relevant area in the code for the hand for example this part :

    Code (csharp):
    1.  
    2.  if (primaryTarget.interactableMode == InteractableItem.InteractableMode.ActionWithoutThrow)
    3.                 {
    4.                     animator.SetIKRotationWeight(AvatarIKGoal.RightHand, finalLookWeight);
    5.                     animator.SetIKRotation(AvatarIKGoal.RightHand, rotationtoTarget);
    6.                     animator.SetIKPositionWeight(AvatarIKGoal.RightHand, finalLookWeight * 1f * closestLookWeight);
    7.                     animator.SetIKPosition(AvatarIKGoal.RightHand, primaryTarget.transform.position);
    8.                    
    9.                 }
    10.  
    The result is :



    The player is pointing with the hand the target but it looks like the hand is in a boxing position or something like that. Instead, I want to make it more realistic something the hand will be more open and the player will point the target with his finger.
     
    ROBYER1 likes this.