I am currently prototyping a top down 3D game in Unity 2021.3.4f1.

My character moves with standard "WASD" and always faces towards my cursor's location on the screen. The camera is fixed and wont rotate with the character at all. Simply described in the picture below: enter image description here

My current solution to roll at the correct angle is with a 2D freeform Cartesian animator blend tree, please see below attached SS on set up and script to control the Parameters: MoveX and MoveZ: enter image description here enter image description here

(Please disregard the * -1 in the GetAxis, its simply corrected for my character that's 180 degrees rotated in game)

Basically moving character "up" with "W" and pressing roll bind with roll the character "up". No problem so far.

My issue with the presented solution is when the character starts to rotate towards the cursor. GetAxis works in local coordinates described in below picture:

enter image description here

As soon as I rotate my character the coordination system rotates with it locally: enter image description here

This causes major issues with my roll animation that uses root motion and basically dash the character in its local coordinate system, but I want it to dash in my world coordinates. Trying to describe in the some what confusing picture below: enter image description here

This video is showcasing the issue in game.

You can clearly see in first rolls that the character rolls in the direction that the character is moving. When rotating 180 degrees its exactly mirrored.

My first thought since I'm not a to good coder is to do a massive If/else statement controlling my MoveX and MoveZ parameters when moving with WASD. Or I replace the Input.GetAxis to get it in world coordinates (haven't found any way to do this)...

What is the simplest and most elegant way of achieving above results?

  • \$\begingroup\$ Remember to share code as text, not as an image. Are you looking for how to convert your input in worldspace into a corresponding local space vector to drive your animations, using eg. Transform.InverseTransformVector? \$\endgroup\$
    – DMGregory
    Dec 11, 2022 at 18:20
  • \$\begingroup\$ Thanks for the heads up! Basically I just want to come up with a way of detecting what way my character is moving in world coordinates and use those values in my blend tree. \$\endgroup\$ Dec 11, 2022 at 18:40
  • \$\begingroup\$ If the character is moving in X = -1 in world coordinates i grab this values to my blend tree. And if my character stands still its X= 0 and Z = 0. \$\endgroup\$ Dec 11, 2022 at 18:41

1 Answer 1


If I understand the problem correctly, you just need to transform the desired world-space movement into a local-space vector to feed it into the blend tree, yes?

In that case, something like this could work

Vector3 worldMovement = new Vector3(moveX, 0, moveZ);
Vector3 localMovement = transform.worldToLocalMatrix.MultiplyVector(worldMovement);
animator.SetFloat("MoveX", localMovement.x);
animator.SetFloat("MoveZ", localMovement.z);
  • 1
    \$\begingroup\$ Couldn't you replace transform.worldToLocalMatrix.MultiplyVector with transform.InverseTransformVector? \$\endgroup\$
    – Philipp
    Dec 15, 2022 at 12:16
  • \$\begingroup\$ Sure, it seems to be the same thing. Although I do like that the matrix explicitly says what the transformation is in the name. \$\endgroup\$
    – PepeOjeda
    Dec 15, 2022 at 22:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .