Make input relative to rotation

+2 votes
asked Aug 27, 2016 in Math by Skurniekto Student (370 points)
So, i have a 2D blend tree where is my fully animated character (walking forward backward, running, etc...). I am controlling the blend tree with Horizontal and Vertical Input axis. The problem is, the animations play correctly only when the character is facing forward (y rot = 0). When he faces left (y rot = 270) and i press D, he starts walking in the up direction. My problem is that i dont know how to convert the input so that he would walk in the right direction(y rot = 90). I tried many things and none have worked. Thanks.

1 Answer

+1 vote
answered Aug 27, 2016 by Bugstacker Senior (4,800 points)
selected Aug 27, 2016 by Skurniekto
Best answer

You have to handle the input like a vector in 2D space. Then transform it to the characters local space. Fortunately this can be done very easily in 2D space. The animation should then be blended using the transformed input vector.

    float x = Input.GetAxis("Horizontal");
    float y = Input.GetAxis("Vertical");

    Vector3 worldInput = new Vector3(x, y, 0);
    Vector3 localInput = transform.InverseTransformDirection(worldInput);
Input transformation


commented Aug 27, 2016 by Skurniekto Student (370 points)
Tried it but it doesnt fully work. For example when im looking right (y = 90) and press A or D it doesnt move horizontally, it doesnt move at all. Any ideas?
commented Aug 27, 2016 by ismix Senior (1,150 points)
I think the coordinates have to be switched. What is your characters forward axis? Could you post a link to a video or a screenshot?
commented Aug 27, 2016 by Skurniekto Student (370 points)
Yeah you were right! Damn bugstacker, youre a lifesaver! Thank you! :)
Welcome to Bugstacker Q&A, where you can ask questions concerning Game Development, Programming and Design and receive answers from other members of the community.

Help for