Search Unity

Prefab animation works in preview, but not in game

Discussion in 'Animation' started by ScramUK, Jan 10, 2021.

  1. ScramUK

    ScramUK

    Joined:
    Jan 10, 2021
    Posts:
    2
    I have created an animator controller which contains a blend tree of type "2D Freeform Cartesian". The blend tree holds two parameters 'Grip', and 'Trigger' and appears to animate the hand prefab as I would expect when I pass the prefab into the preview window like so :
    upload_2021-1-10_0-18-3.png

    I have added an animator and the 'Left Hand Animator' controller to the prefab :

    upload_2021-1-10_0-21-51.png

    Here is some of the initialisation code where the 'handGameObject' here is a created instance of the 'Left Hand Model' prefab that's instantiated.

    Code (CSharp):
    1.         handGameObject = Instantiate(handModelPrefab, transform);
    2.         handAnimator = handGameObject.GetComponent<Animator>();
    And here is the code I am using to update the animations 'Grip' and 'Trigger' parameters :

    Code (CSharp):
    1.     private void UpdateHandAnimation()
    2.     {
    3.         if (tgtDevice.TryGetFeatureValue(CommonUsages.trigger, out var triggerValue))
    4.         {
    5.             handAnimator.SetFloat("Trigger", triggerValue);
    6.         }
    7.  
    8.         handAnimator.SetFloat("Trigger", 0.0f);
    9.  
    10.         if (tgtDevice.TryGetFeatureValue(CommonUsages.grip, out var gripValue))
    11.         {
    12.             handAnimator.SetFloat("Grip", gripValue);
    13.         }
    14.  
    15.         handAnimator.SetFloat("Grip", 0.0f);
    16.     }

    The problem is, I cannot get the hand models to animate in game. I have checked that 'triggerValue' and 'gripValue' are getting populated correctly at runtime - which they are.
    Is there something I am missing?
     

    Attached Files:

  2. ScramUK

    ScramUK

    Joined:
    Jan 10, 2021
    Posts:
    2
    It's okay..... I was able to find the issue, I was being an idiot. The issue was in the code itself :
    handAnimator.SetFloat("Trigger", 0.0f);
    and
    handAnimator.SetFloat("Grip", 0.0f);
    should be within their respective else statements.