- Home /
Animator movement and mouse position problem
Hey, I've been struggling with this problem for a few days now, and I'm starting to lose my sanity here.
The problem is that I have a character in game and an animator, with animations, attached to it. The character looks at the current mouse position, so the transform.forward is towards the mouse. The character turning works as intended but another problem arises when I try to move the player according to world coordinates instead of his current rotation.
I have this basic type of a script as for now:
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
PlayerMovement(v, h);
My PlayerMovement function is declared elsewhere, but the problem is that I want for the player to move, ie. when pressing up, to move up (z-axis +). Currently, when pressing up, the player moves towards the mouse position (transform.forward), because the animator's floats are made in such way. In the PlayerMovement function I send the h and v floats to my animator which has values according to the two of them.
What I am trying to achieve, is to convert the floats h and v, so that it takes the player's rotation into account. The character should, like I said earlier, move up when I press up, left when I press left et cetera according to world, not it's own rotation.
I hope you understand what I'm trying to achieve. For example, if you have horizontal 1.0f and vertical 0.0f coming from the player input (player is walking right), the animator floats should be 1 and 0 respectively if the player's rotation.y is equal to 0. If however, the player's rotation.y is for example 90 degrees, the animator values should be 0 and 1 (ie. the player is walking forward, because he is looking right).
I think that the two calculations (for the animator's horizontal and vertical variables) should both use the h, and v and the player's rotation. I've tried using sin and cos functions, but I still can't get my head around it. There might be some Mathf-function which will solve this easily, but I sure as hell know nothing about it.
Damn, this ended up as a wall of text, but I thank you in advance!
Your answer
Follow this Question
Related Questions
can anyone find the issue with this code? 2 Answers
How can I move an object to click point in 2D? 0 Answers
Why my player don't go forward? 0 Answers
My walking animations root motion has a problem when my mouse look script is being used? 0 Answers
Gradual Rotation of Character from 2 Axis Input Sources 0 Answers