Search Unity

Changing the 'orientation' of the InputAxis for inputs like "Horizontal" and "Vertical" input Axis

Discussion in 'Scripting' started by Galactic_Muffin, Jan 19, 2016.

  1. Galactic_Muffin

    Galactic_Muffin

    Joined:
    Aug 14, 2013
    Posts:
    67
    So because of the way my camera works Unity's axis along X and Z are correct for tiling. However lets say i wanted to have a ship you could fly around using the "Horizontal" and "Vertical" input keys, if i want it to feel natural, the "W" key would make the ship move forward in relation to the camera instead of Unities world axis as i have depicted in this graphic (Green is what i want, and the red and blue is what it currently does):

    So how do I get the ship to move forward in this manor instead of being based on the X Y and Z axis that the InputAxis already recognize?

     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    unfortunately I cant see the image due to company firewall.

    However, let me explain by saying that you want to do this by using the ships direction. So Horizontal would control left/right or the y rotation (if you are looking from the bottom) or x rotation (if you are looking at the side) Vertical would always control the forward direction.

    This tutorial (the script part about moving) will help to understand how these are applied.

    http://clarksvillegamedesign.net/learn/tutorials/worm-game/

    Code (csharp):
    1.  
    2. void Update () {
    3.     float horizontal = Input.GetAxis("Horizontal");
    4.         Vector3 rotate = new Vector3(0, horizontal * Time.deltaTime * 60, 0);
    5.         transform.Rotate(rotate);
    6.  
    7.     float vertical = Input.GetAxis("Vertical");
    8.     Vector3 move = transform.forward * vertical;
    9.  
    10.     CharacterController controller = gameObject.GetComponent<CharacterController>();
    11.     controller.SimpleMove(move);
    12. }
    13.  
     
  3. Galactic_Muffin

    Galactic_Muffin

    Joined:
    Aug 14, 2013
    Posts:
    67
    Thanks for the reply but I think you may need to view the image. I apologize it is very hard for me to explain this specific issue without a visual aid. lol
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    I wouldn't change what the axes does. Instead, I would rotate them to fit the camera.

    If your camera is rotated 45 degrees on the y-axis, your horizontal axis would be something like:

    Code (csharp):
    1. float horizontal = Input.GetAxis("Horizontal");
    2. float vertical = Input.GetAxis("Vertical");
    3.  
    4. Vector3 inputVector = new Vector3(horizontal, 0, vertical);
    5. Vector3 rotated = inputVector * Quaternion.Euler(0, 45, 0);
    The rotated vector is the input vector, rotated 45 degrees. It might be that you actually want -45, I'm terrible at doing rotations in my head.

    Man, US companies. How do you even live with that?
     
  5. RickshawDerpyDerp

    RickshawDerpyDerp

    Joined:
    Nov 6, 2018
    Posts:
    25
    Line 5 doesn't compute, Visual Studio says you can't multiply a vector by a quaternion. Any idea how to get around this?
    EDIT: Problem is one of dimension size. If inputVector is 3x1 and Quaternion is 3x3, and because interior dimensions must match to multiply vectors and/or matrices, the order should be

    Code (csharp):
    1. Vector3 rotated = Quaternion.Euler(0,45,0) * InputVector;
    This works in my project. Thanks
     
    Last edited: Jun 10, 2019
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    You're right in that it works, but not why. All quaternions are four dimensional (eg. 4x1) vectors with some additional restrictions. 3x3 would be a matrix. The error I made was order of operations.

    There's a definition for quaternion * vector, but not one for vector * quaternion.