Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by CryptoJanne, May 31, 2020.

  1. CryptoJanne

    CryptoJanne

    Joined:
    Jul 7, 2019
    Posts:
    12
    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
    Code (CSharp):
    1.  
    2. vertical = Input.GetAxis("Vertical");
    3. horizontal = Input.GetAxis("Horizontal");
    4.  
    and then feed it to the animator
    Code (CSharp):
    1.  
    2. animator.SetFloat("Horizontal", horizontal);
    3. animator.SetFloat("Vertical", vertical);
    4.  
    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!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    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

    https://gitlab.com/kurtdekker/proximity_buttons

    https://sourceforge.net/projects/proximity-buttons/
     
  3. CryptoJanne

    CryptoJanne

    Joined:
    Jul 7, 2019
    Posts:
    12
    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.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Actually if you read the code it's optional. If you don't provide one it will still work.

    Code (csharp):
    1.     // optional; put one of these on here if you want to have its benefits
    2.     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
     
    Last edited: May 31, 2020
  5. CryptoJanne

    CryptoJanne

    Joined:
    Jul 7, 2019
    Posts:
    12