KybernetikGames / animancer

Documentation for the Animancer Unity Plugin.
63 stars 8 forks source link

The proper way to set a state's weight while keeping the total weight 1? #308

Closed kodra-dev closed 11 months ago

kodra-dev commented 11 months ago

For example:

I tried to set

stateB.Weight = 1;

But then A and B both have weight of 1 and I don't want that. I'd like to set B's "normalize weight" to 1, in other words all the other states should adjust their weights accordingly.

Of course in this simple case I could do:

stateA.Weight = 0;
stateB.Weight = 1;

But what if I don't have a reference to state A? What if there are more than one existing states?

KybernetikGames commented 11 months ago

You could iterate through all the states on that layer like this:

var layer = Animancer.Layers[0];
var count = layer.ChildCount;
for (int i = 0; i < count; i++)
{
    var state = layer.GetChild(i);
    state.Weight = state.TargetWeight;
}
kodra-dev commented 11 months ago

I see. Works like a charm!