Forum rules - please read before posting.

Change Mecanin animation Layer weight

Hello Chris,
I could not find a way (through action lists) to change the weight of an Animator layer.

Say i want the player to pick up a box. When this happen, rather than triggering a custom animation, i want instead to set the weight of the masked layer containing the additive pose to 1.

My player is 3D and is using Unity Mecanim's animation system.

Thanks!

Comments

  • edited December 2021

    Here's a custom Action, Player: Set layer weight, that you can plug into your Actions Manager.

    Name the file ActionPlayerWeight.cs, and place in a new Assets folder.

    using UnityEngine;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
    
        [System.Serializable]
        public class ActionPlayerWeight : Action
        {
    
            public int layerIndex;
            public float newWeight;
    
            public override ActionCategory Category { get { return ActionCategory.Player; }}
            public override string Title { get { return "Set layer weight"; }}
    
            public override float Run ()
            {
                KickStarter.player.GetAnimator ().SetLayerWeight (layerIndex, newWeight);
                return 0f;
            }
    
            #if UNITY_EDITOR
    
            public override void ShowGUI ()
            {
                layerIndex = EditorGUILayout.IntField ("Layer index:", layerIndex);
                newWeight = EditorGUILayout.FloatField ("New weight:", newWeight);
            }
    
            #endif
    
        }
    
    }
    
  • Thanks Chris :)

  • Hey there! I found this script and could use it for my project. Thing is, the script gives me the error 'ActionPlayerWeight.Run() not all code paths return a value. Sorry if this is a minor issue. Just started to get into cs aswell. Cheers!
  • Welcome to the community, @Voxxy.

    It's my fault - I made a typo in the code. I've corrected the post now.

  • Thanks alot for the lightning reply, @ChrisIceBox! And thanks for your awesome adventure creator. Will this script work in sprites unity complex aswell?
  • It should, yes.

  • Works perfectly now! Thanks!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.