Adjusting blend tree horizontal and vertical parameters based on rotation angle

This is related to my previous post in the animation section, but my issue is related to code so this post was in the wrong forum (Adjusting blend tree parameters based on 3 dimensions (horizontal, vertical and rotation). - Unity Engine - Unity Discussions).

I have a rather complex problem with 2d animation blend trees that is doing my head in.

I am building a rather simple top down isometric style game, similar to the “Survival shooter” tutorial by the Unity guys.

The main difference I have at this stage, is that I am using more complex humanoid character models that have movement animations for 8 degrees. I have built a blend tree for this that operates on the Horizontal and Vertical input (WASD)… this works great (see attachment).

However, the player (and other game characters) can be facing/aiming in any direction within 360 degrees, while moving in any direction within 360 degrees.

So if the player is facing 0 degrees and hits the W key he correctly moves and visually appears to be moving forward… however if the player is facing 180 degrees and presses W, he is now correctly moving backwards but he still appears to be moving forwards.

It’s a little difficult to explain, but I hope this is clear?

I have come to the conclusion that this needs to be handled in code, so effectively the only way that is clear to me is to apply a modifier to the horizontal and vertical output to flip the output based on direction of angle.

Here is a rough code example of what I need:

float h = Input.GetAxisRaw ("Horizontal");
float v = Input.GetAxisRaw ("Vertical");
float direction = Quaternion.LookRotation(rigidBody.transform.forward).eulerAngles.y

// psuedo code just to demonstrate problem
if (direction > 0 && direction < 180)
{
    v = v-1; //flip the axis value
}

animatorController.SetFloat ("Horizontal", h);
animatorController.SetFloat ("Vertical", v);

I’m totally stuck here, please help!

try this

    float h = Input.GetAxisRaw ("Horizontal");

    float v = Input.GetAxisRaw ("Vertical");

    float direction = Quaternion.LookRotation(rigidBody.transform.forward).eulerAngles.y

    // psuedo code just to demonstrate problem

    if (direction > 0 && direction < 180)

    {

        v = -v; //flip the axis value
        h = -h;

    }

    animatorController.SetFloat ("Horizontal", h);

    animatorController.SetFloat ("Vertical", v);

if the v is 1 you set to 0 soctracting 1

Hey thanks for your help, the code I posted is not relevant to the issue. It was just to help explain the problem as it is difficult to understand…

did you find any solution. i have exact same problem.

This is a couple years old, but since no solution made its way here and I came stumbling by, later chancing upon a solution, figured I’d share it here for any other wandering devs:

Happy animating!