Root motion, want to face the direction that the camera looks.

Hello everybody, I have decided to try out root motion! I have an issue though that i have googled around for HOURS trying to find.

I can control my character using

vertical = Input.GetAxis("Vertical");
horizontal = Input.GetAxis("Horizontal");

and then feed it to the animator

animator.SetFloat("Horizontal", horizontal);
animator.SetFloat("Vertical", vertical);

but that does move the character totally unrelated to the camera look position. I want the character to turn and move towards where the camera is looking and i cannot figure out how i can get a float value between 1 and -1 that i can feed the animator.

Any help deeply appreciated!

This is a very common question lately… you have to gather the inputs into a Vector3 and rotate that by the heading of the camera.

Fortunately I have a complete sample project for this in my proximity_buttons package. Look for the DemoCameraRotatedControls.unity scene:

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

https://github.com/kurtdekker/proximity_buttons

I can see what you did, but you are using a CharacterController. I am using root motion with turn animations regulated by float horizontal, -1 → 1. Do i make myself understood? I basically need to get either 1 or -1 (whichever is closest) until both the camera and player is facing the same direction.

Actually if you read the code it’s optional. If you don’t provide one it will still work.

    // optional; put one of these on here if you want to have its benefits
    CharacterController cc;

Are you looking for this?

https://docs.unity3d.com/ScriptReference/Mathf.DeltaAngle.html

or this?

https://docs.unity3d.com/ScriptReference/Mathf.MoveTowardsAngle.html

Thank you :slight_smile: