I just need to change one Animation Layers's weight from 0 to 1 at the press of a button

I’m thinking it should be simple…but I’m just an artist and I need this stuff just to help build my portfolio, so what I getting so far is having the character getting into the Attack Layer but never coming out. I need to press “q” for having the Attack Layer fully weighted. It does work fine with GetKey, but I want it to stay that way until I press “q” again to get him out. Could someone please give me a hint at least. Cheers!

// Start is called before the first frame update
void Start()
{
any_controller = GetComponent();
any_controller.SetLayerWeight(0, 1);
}

// Update is called once per frame
void Update()
{

if (Input.GetKeyDown(“q”))
{
any_controller.SetLayerWeight( any_controller.GetLayerIndex(“Attack Layer”), 1);
}
else if (!Input.GetKeyDown(“q”))
{
any_controller.SetLayerWeight( any_controller.GetLayerIndex(“Attack Layer”), 0);
}

If the above is the correct way to set the layer (I don’t know this but I’ll take your word), all you need to change is to make a bool in your class (not in your method!):

private bool InAttackMode;

then in your Update():

// toggle attack mode
if (Input.GetKeyDown("q"))
{
  InAttackMode = !InAttackMode;
}

// now check and act on it:

if (InAttackMode)
{
  any_controller.SetLayerWeight( any_controller.GetLayerIndex("Attack Layer"), 1);
}
else
{
  any_controller.SetLayerWeight( any_controller.GetLayerIndex("Attack Layer"), 0);
}

See how that flips the bool from true to false, then acts on it separately based on what it is?

It worked perfectly an the way you wrote this, makes sense to me( “writing” code for 4 days now). Thank you so much!!!

1 Like