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

Question How to turn input axes to vectors, relative to the way an object is facing?

Discussion in 'Scripting' started by TheMatrixAgent22, Dec 17, 2022.

  1. TheMatrixAgent22

    TheMatrixAgent22

    Joined:
    Jul 9, 2017
    Posts:
    41
    Hello!
    I am currently working on a procedural walking animation script.
    To calculate where the next step will be, I am currently using:
    Code (CSharp):
    1. lastStep = newStep;
    2. newStep = transform.position + (transform.InverseTransformDirection(movementNormal) * stepLength);
    3. newStep.y = RaycastGround(newStep).point.y;
    movementNormal
    is a Vector3, made from
    new(Input.GetAxisRaw("MovementX"), 0, Input.GetAxisRaw("MovementY"));


    When the player faces the global z axis, the steps are placed correctly. However, when the player faces the global x axis, the steps seem to be offset to the right. This is the only pattern I noticed. The script acts wonky all the time.

    Seemingly, the problem is that I can't get the
    movementNormal
    to successfully translate into local a local direction.
    Can anyone help me?
    Thank you!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    The idea is to multiply the input vector by the rotation in question, then use that rotated vector instead.

    Sometimes you flatten the rotation (eg, use only its heading).

    You can see this happening here, around lines 124 to 130:

    https://github.com/kurtdekker/proxi...edControls/RotatedControlsPlayerController.cs

    Demo scene in the full project... 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/
     
    TheMatrixAgent22 likes this.
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
  4. TheMatrixAgent22

    TheMatrixAgent22

    Joined:
    Jul 9, 2017
    Posts:
    41
    Hi!
    Thanks a lot for the quick reply.
    I don't really understand the script you quoted.
    Basically what that line does is:
    Vector3 rawInput * Quaternion.Euler(0, transform.eulerAngles.y, 0)
    but like expected, that yields an operator error. You can't multiply a Vector3 by a Quaternion.
    Also, simply multiplying
    rawInput 
    (in my case
    movementNormal
    ) by
    transform.rotation.y
    also doesn't get a better result.
    Something I'm missing here?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Yes, yes you can, and you didn't copy the code I provided correctly.

    It must be
    rot = quat * rot


    It must NOT be
    rot = rot * quat


    See this line:

    Code (csharp):
    1. var RotatedMoveInputs = controlRotation * RawMovementInputs;
    It works, really. Trust me, I've been rotating vectors with quaternions for a long time. :)
     
    spiney199 likes this.
  6. TheMatrixAgent22

    TheMatrixAgent22

    Joined:
    Jul 9, 2017
    Posts:
    41
    Haha!!! I would've never thought that the order of the multipliers mattered!!! The more you know :)
    Thanks a lot, it works just as intended.
    Have a great day!
     
    Kurt-Dekker likes this.