Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Help Wanted SetLayerWeight Not Working

Discussion in 'Animation' started by MissterM, Jul 24, 2021.

  1. MissterM

    MissterM

    Joined:
    May 4, 2021
    Posts:
    2
    Hi, I tried today do crounching system in my game with second layer in animator. Both layers are Override. First time I made it but I have to add when player stops pressing left ctrl, weight going to 0. When i do that, Weight of second layer dont even get up. I check with Debug.Log if the program see left ctrl press, and every work. I think the problem is in command SetLayerWeight. I havent any errors. What I have to change? Thank u in advance.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TwoDController : MonoBehaviour
    6. {
    7.     Animator anim;
    8.    public float acceleration = 2.0f;
    9.  
    10.     void Start()
    11.     {
    12.         anim = GetComponent<Animator>();
    13.     }
    14.  
    15.    void Crounching(bool crounchPressed, float crounchWeight)
    16.    {
    17.         if (crounchPressed)
    18.         {
    19.            crounchWeight += Time.deltaTime * acceleration;
    20.         }
    21.  
    22.     void Update()
    23.     {
    24.     bool crounchPressed = Input.GetKey("left ctrl");
    25.    float crounchWeight = 0.0f;
    26.  
    27.    Crounching(crounchPressed, crounchWeight);
    28.  
    29.    anim.SetLayerWeight(anim.GetLayerIndex("Kucanie"), crounchWeight);
    30.   }
    31. }
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    204
    Hey,

    You should actually have an error with the script you posted. You're missing a semicolon right before Update().

    Another issue is that your "Crounching" method is doing calculations for something, but they never get applied to anything because the "crounchWeight" variable is local to Update().

    The solution to this is to move the "crounchWeight" variable to the top of the script right under "acceleration." Finally, you shouldn't pass "crounchWeight" into the "Crounching" method because it will just mess things up.

    This is what it looks like after the above fixes:
    Code (CSharp):
    1. {
    2.     Animator anim;
    3.     public float acceleration = 2.0f;
    4.     float crounchWeight = 0.0f;
    5.  
    6.     void Start()
    7.     {
    8.         anim = GetComponent<Animator>();
    9.     }
    10.  
    11.     void Crounching(bool crounchPressed)
    12.     {
    13.         if (crounchPressed)
    14.         {
    15.             crounchWeight += Time.deltaTime * acceleration;
    16.         }
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         bool crounchPressed = Input.GetKey("left ctrl");
    22.  
    23.         Crounching(crounchPressed);
    24.  
    25.         anim.SetLayerWeight(anim.GetLayerIndex("Kucanie"), crounchWeight);
    26.     }
    27. }
    I have no idea if it will actually work for what you need it to, but the "crounchWeight" variable actually increases now when you hold the Control key, and the Kucanie layer does smoothly increase to 1 over time.

    Also, as a heads up, it's actually spelled "crouch," not "crouNch.

    Good luck!
     
    MissterM likes this.
  3. MissterM

    MissterM

    Joined:
    May 4, 2021
    Posts:
    2
    Yes, Its working! Thank you very much! Yesterday I tried fix that for 3 hours and I cant found misstake. Probably when I did this for the first time and its worked, next I added "crounchWeight" to "Crounching" method, cause I thought if I using this variable I have to add they. Thanks again! <3
     
    Unrighteouss likes this.
unityunity