How can I smoothly shift Animator Layer Weights via transform.eulerAngles?

Hey guys, I’m very new to scripting and there is probably more efficient ways to do the things I’m doing, regardless I’m stuck now.

I’ve got a character moving around WASD style, and have him facing the cursor position at all times.

I want the character to be walking forward towards the bottom of the screen when the cursor is at the bottom of the screen (while pressing “S”), BUT walking backwards towards the bottom of the screen when the cursor is at the top of the screen (also pressing “S”).

I’ve got 8 animation layers all going at different orientations and I want to drive which one is weighted highest smoothly passing on from the previous depending on the orientation of the character. think

  • “0deg” as full “Layer 0” (no others)"
  • 45deg" as full “Layer 1”
  • “90deg” as “Layer 2” etc

but

  • “22.5deg” would be halfway between “Layer 0” and “Layer 1” weighting.

I’ve got transform.eulerAngles.y ready to feed the current angle (0-360) and just need to hook it up to the Animator Layers.

I’ve got Animator.SetLayerWeight(layer#,weight[0-1]) ready to be changed - just need the missing link!

Thanks for your time+attention!

If you take Transform.forward, cast it to Vector2 to get rid of the z component, and make a dot product with the direction you want to represent, you should get value that goes between 0 and 1.

float w0 = Vector2.Dot ((Vector2)transform.forward, Vector.up);
Animator.SetLayerWeight(0, w0);
float w1 = Vector2.Dot ((Vector2)transform.forward, Vector.right);
Animator.SetLayerWeight(1, w1);
//etc

Although, I would rather use a Blend Tree for this.