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:
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
:
(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:
As soon as I rotate my character the coordination system rotates with it locally:
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:
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?