Search Unity

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

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

  1. alvinsoft

    alvinsoft

    Joined:
    Jul 9, 2017
    Posts:
    50
    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:
    38,745
    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/
     
    alvinsoft likes this.
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,935
  4. alvinsoft

    alvinsoft

    Joined:
    Jul 9, 2017
    Posts:
    50
    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:
    38,745
    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. alvinsoft

    alvinsoft

    Joined:
    Jul 9, 2017
    Posts:
    50
    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.