Search Unity

Question [Fixed] Having trouble setting Rig's weight through script

Discussion in 'Animation Rigging' started by KenjiJU, Aug 27, 2022.

  1. KenjiJU

    KenjiJU

    Joined:
    Dec 31, 2012
    Posts:
    21
    Hello.
    I'm trying to set rig.weight = 1f where my character starts aiming inside of Update, but it never changes the value. The value change works if I place it somewhere outside of aiming.

    I can manually set the rig's weight before hitting Play, so I can see the end result of what I want with the ARigging working, but I also cannot change the rig's weight on the fly.

    Any ideas what's preventing me from changing the value here?

    Code (CSharp):
    1.     private void Update() {
    2.  
    3.         Vector3 mouseWorldPosition = Vector3.zero;
    4.  
    5.         Vector2 screenCenterPoint = new Vector2(Screen.width/2f, Screen.height /2f);
    6.         Ray ray = Camera.main.ScreenPointToRay(screenCenterPoint);
    7.         if(Physics.Raycast(ray, out RaycastHit raycastHit, 999f, aimColliderLayerMask)) {
    8.             debugTransform.position = raycastHit.point;
    9.             mouseWorldPosition = raycastHit.point;
    10.         }
    11.  
    12.         if (starterAssetsInputs.aim)
    13.         {
    14.             crossHair.gameObject.SetActive(true);
    15.             aimVirtualCamera.gameObject.SetActive(true);
    16.             thirdPersonController.SetSensitivity(aimSensitivity);
    17.             thirdPersonController.SetRotateOnMove(false);
    18.  
    19.             animator.SetLayerWeight(1, Mathf.Lerp(animator.GetLayerWeight(1), 1f, Time.deltaTime * 10f));
    20.  
    21.             Vector3 worldAimTarget = mouseWorldPosition;
    22.             worldAimTarget.y = transform.position.y;
    23.             Vector3 aimDirection = (worldAimTarget - transform.position).normalized;
    24.             transform.forward = Vector3.Lerp(transform.forward, aimDirection, Time.deltaTime * 20f);
    25.  
    26.             rig.weight = 1f;
    27.             Debug.Log("Aiming");
    28.  
    29.             if(starterAssetsInputs.shoot)
    30.             {
    31.                   //shooting stuff
    32.             }
    33.             return;
    34.         }
    35.         else
    36.         {
    37.             crossHair.gameObject.SetActive(false);
    38.  
    39.             aimVirtualCamera.gameObject.SetActive(false);
    40.             thirdPersonController.SetSensitivity(normalSensitivity);
    41.             thirdPersonController.SetRotateOnMove(true);
    42.  
    43.             animator.SetLayerWeight(1, Mathf.Lerp(animator.GetLayerWeight(1), 0f, Time.deltaTime * 10f));
    44.  
    45.             rig.weight = 0f;
    46.  
    47.             Debug.Log("Not Aiming");
    48.         }
    49.     }
    It looks like there's conflict with when changing the animation layer through the script at the same time, but I'm able to have the second layer working along with manually setting the rig's weight to 1, so I don't understand why this isn't working.

    Proof of manually setting the weight to 1 with the second animation layer active:

    The spine bone is affected by ARigging to get the vertical body rotation and the arms are animated by the idle anim in the Aiming layer.

    What could be causing me to be unable to change the rig weight through script?
     
    Last edited: Aug 28, 2022
  2. KenjiJU

    KenjiJU

    Joined:
    Dec 31, 2012
    Posts:
    21