Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to get the correct Input direction based on the camera angle to use in a rootmotion based third

Discussion in 'Scripting' started by Aladine, Jan 16, 2021.

  1. Aladine

    Aladine

    Joined:
    Jul 31, 2013
    Posts:
    195
    Hi,

    I move my character based on the rootmotion of their animation using a blend tree that uses 2D Freeform Cartesian input (Vertical, Horizontal)

    if Vertical = 1, the character moves forward based on the direction he is facing

    if Horizontal = 1, the character turns right based on the direction he is facing

    and the rest of the system follows, here is the current Blend Tree:

    Currently, if i press forward, the character move forward no matter where the camera is facing:

    But in this example, what i want is for the character to slightly make a turn left to face the camera direction and then start walking forward:

    When working on a game that moves the character by code (without rootmotion), something like this would solve the issue:
    Code (CSharp):
    1. _inputAxis = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
    2. _inputCam = Quaternion.Euler(0, _cam.transform.eulerAngles.y, 0) * _inputAxis;
    And then use _inputCam to move the character, but for some reason this isn't working, and these two variables are always equal to each other, what am i doing wrong ? also if there is a "standard" solution for this problem (am sure it is) please link it (doesn't matter if its Unity or not as long as the code is readable)

    Thanks!