Hope someone can help me with this, it seems like it should be a simple problem but that only makes it more frustrating!

I have a simple game where you roll a ball around by applying force to the horizontal and vertical axes. What I want to happen is, on a trigger, the axes switch from say the X and Y, to the X and Z, changing the way the player controls without changing the input buttons... The script I use to move the player is below:

  float moveHorizontal = Input.GetAxis ("Horizontal");
  float moveVertical = Input.GetAxis ("Vertical");

  Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
  rb.AddForce (movement * speed);

(Where rb is the player ball object) - I'm thinking I'm going to have to change the way the player is controlled as I can't any reference to a 3rd axis in the Input.GetAxis documentation... any help with this would be great!


  • \$\begingroup\$ So your Vector3 movement code is probably the what needs to change if you want the player controls to be the same but the movement to be different. Right now you are applying the movements to the (horizontal, 0f, vertical) positions, if you want to make the vertical movement affect the y axis instead then it needs to be new Vector3(horizontal, vertical, 0f); or something along those lines \$\endgroup\$
    – Latency
    Commented Jun 1, 2015 at 16:42
  • \$\begingroup\$ This works!! Thank you so much for the simple fix, now I just need to work out how to change it from a boolean variable to like multiple variables or something (I will need 6 states) - Thanks again, sorry for my stupidity everyone. \$\endgroup\$ Commented Jun 1, 2015 at 17:40

1 Answer 1


How about something like this:

public class TriggerMovement : MonoBehaviour 
    float speed;

    enum eAxis
        Normal = 0,


    Vector3[] horizontalAxes = new Vector3[(int)eAxis.MAX];
    Vector3[] verticalAxes = new Vector3[(int)eAxis.MAX];

    eAxis curAxis = eAxis.Normal;
    void Start()
        //Setup all axes you want to use here ...
        horizontalAxes[0] = new Vector3(1f, 0f, 0f);
        horizontalAxes[1] = new Vector3(0f, 0f, 1f);

        verticalAxes[0] = new Vector3(0f, 1f, 0f);
        verticalAxes[1] = new Vector3(1f, 0f, 0f);

    void doMovement(Rigidbody rb)
        //Get Input
        float moveHorizontal = Input.GetAxis("Horizontal"); 
        float moveVertical = Input.GetAxis("Vertical");

        //Set input on appropriate axes
        Vector3 horizontalMovement = horizontalAxes[(int)curAxis] * moveHorizontal;
        Vector3 verticalMovement = verticalAxes[(int)curAxis] * moveVertical;

        //Get total movement
        Vector3 movement = horizontalMovement + verticalMovement;

        //Scale by speed
        rb.AddForce(movement * speed);

If you define the axes you want to use up front then the movement method could make use of the currently selected axes and apply the moevement from the triggers to those axes. To switch the axes you would just need to change the curAxis enum.

I haven't run this code but believe it should do what you want. Any questions just ask :)

  • \$\begingroup\$ Hiya thanks for the speedy reply! I'm new to C#, all my previous coding experience is webdesign in javascript and html... so please bare with me :P I think I understand what you are trying to do, we designate each possible axis up front and then use a variable to call in the ones we want on the trigger. Is the enum the equivalent of the variable? How do I properly assign these? Sorry for being a bit dense, I've added your script into a test player, got all the classes working so there are no errors, I now just need to get it moving! \$\endgroup\$ Commented Jun 1, 2015 at 17:02
  • \$\begingroup\$ Well yes the axes you want to use. Presumably you'll only want to change to a few different axes. An enum is basically just an int (normally). This means we can take advantage of the nice names it gives us but cast it to an int to index into an array which is what I did there. You can see that I've set the first value to 0 and all subsequent values will increment by 1 so MAX tells us the maximum number of elements in the array. Another way I could have written that setup is 'horizontalAxes[(int)eAxis.Normal] = new Vector3(1f, 0f, 0f);' Does that make sense? \$\endgroup\$
    – TJClifton
    Commented Jun 1, 2015 at 17:40
  • \$\begingroup\$ So you would just assign the enum variable by doing curAxis = someEnumValue when the player presses the button to switch axes. When that value gets cast to an int in the movement method the correct axes will be pulled out and used to apply the movement. Sorry if this isn't entirely clear it's difficult squeezing a response into these comments with limited characters :) \$\endgroup\$
    – TJClifton
    Commented Jun 1, 2015 at 17:47
  • \$\begingroup\$ Nope, that does make sense! Sorry, I think I just need to do a little more research into C# itself, for the mean time I've got your code working with Boolean variables and it does everything I wanted... thanks :D \$\endgroup\$ Commented Jun 1, 2015 at 17:53
  • \$\begingroup\$ Awesome. If that's answered your question I'd appreciate it if you marked it as the answer :) \$\endgroup\$
    – TJClifton
    Commented Jun 1, 2015 at 18:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .