unity 3d student

Beginner B12 – Input with Axes

How to use control with Arrow keys for left and right input axes.

Code Used

//Basic axis input
  1. function Update () {
  2.         var horiz : float = Input.GetAxis("Horizontal");
  3.  transform.Translate(Vector3(horiz,0,0));
  4. }

Share via Social Media

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Add to favorites


    At 3:08 it should be: “Its moving at 1 unit per frame”, not “1 unit per second”. Big difference!

    • Oops! of course, well spotted. Fixing and reuploading now, thanks Corrie!

    could you make a tutorial like this but instead of having the object move on an axis, have it rotate?


    • Matt, I may do but you can simply substitute Translate for Rotate – take a look at the Transform class in the script reference. Visit the script reference and search for ‘Transform’ and you’ll see all the things you can do with it via scripting.

    @ Beginner B12 – Input with Axes:

    How would you do this for the iPhone? Just out of curiosity. The controllers I have are more complex. the ones that come with iPhone unity. When all I want to do is move side to side. This seems perfect.


    • you’ll need to either establish a touch control or make use of the accelerometer to grab a value, assign this value to a variable, then use the value of the variable to move via translate or rigidbody forces in an Update or FixedUpdate function (the latter if you’re using physics).

    I got how to move forward too 😛
    Here is the code

    var speed = 3.0;
    var rotateSpeed = 3.0;

    function Update ()
     var controller : CharacterController = GetComponent(CharacterControll­er);

    transform.Rotate(0, Input.GetAxis (“Horizontal”) * rotateSpeed, 0);

    var forward = transform.TransformDirection(V­ector3.forward);
    var curSpeed = speed * InputGetAxis (“Vertical”);
    controller.SimpleMove(forward * curSpeed);
    @script RequireComponent(CharacterController)

  • Hey. These modules are great. thank you. so i see that when you selected ‘jump’ from the input menu, it was attached to the x axis. is that something we’d eventually change to y if we want the player to jump vertically?

    • Are you referring to this module? doesn’t seem like it.. confused! Either way Jump as an axis is simple a button so its axis doesnt matter as your actual movement or whatever you’re doing is defined axis wise by the code you write, the button just triggers the action.

    On the video, you mentioned you had to multiply the value a number lower than one. How do I multiply the value?

    • transform.Translate(Vector3(horiz,0,0) * 0.1); but more often you’d use transform.Translate(Vector3(horiz,0,0) * Time.deltaTime * speed); where ‘speed’ is a variable you declare and control yourself.

      • Thank you very much! And this is a wonderful website! I started the week knowing nothing, and ended with a playable game! Kudos to you! ^^

    I’m using this to try and control a ball, and after 5 minutes of scratching my head, I realised that as it started moving, it would rotate, and therefore the axis on the object would change and my ball would be trying to fly into the air…

    Is there a simple way around this? I’d like to maintain the effect of the ball rolling, but to allow someone to control it with WASD.

    Any help would be greatly appreciated. 🙂

    • I’ve kind of solved the problem by:
      – Removing the script from ‘Ball’
      – Creating a blank object: ‘Ball Handler’
      – Placing ‘Ball Handler’ inside of the ‘Ball’
      – Making ‘Ball’ the Child of ‘Ball Handler’
      – Attaching the script to ‘Ball Handler’

      But now I’ve encountered another problem, two actually…

      First the ball doesn’t roll realistically, infact it barely rolls at all when it’s running over the ground.

      Secondly, and more importantly, when the physical ‘Ball’ hits another object, say, ‘Block’. It makes the ‘Ball’ roll, to which I can’t correct using WASD as I only control the ‘Ball Handler’ and not the ‘Ball’ that’s physically rolling.

      Help on either of these would be lovely. And as ever, great work on the tut’s. 🙂

      • Have a look at transformdirection in script ref- you basically need to work in world coordinates rather than local, that’s what’ll be causing majority of your issues.

    Was trying to push the lesson a bit further by adding forward/backward, strafe left/right, and turn left/right to a box (parented under an empty GameObject (which the script is attached to). I can get the box to move forward/backward with no issue. I run into a problem where I can either strafe left/right or turn left/right, but not both. It seems like Unity only allows for one a/d or q/e key binding at a time. I edited the Input on the second Horizontal name to “Strafe” and Alt Neg/Pos keys as q/e

    Any thoughts? Here’s my script.
    var turnSens : float = 2.0;

    function Update ()
    var turn : float = Input.GetAxis(“Horizontal”);
    var vec : float = Input.GetAxis(“Vertical”);
    var horiz : float = Input.GetAxis(“Strafe”);

    Debug.Log(turn + “:” + vec + “:” + horiz);

    transform.Rotate(Vector3(0, turn*turnSens, 0));
    transform.Translate(Vector3(horiz, 0, 0));
    transform.Translate(Vector3(0, 0, vec));


  • The object is moving on the z axis, how do I move it in the x axis? using javascript

    • Use the X value instead of the Z… eg

      Translate(5, 0, 0);

      values like this are always (X, Y, Z);

    This website may be dead, but it’s been a great help so far. I’m thinking about trying to use Unity3D to write a quick 2D platformer and I was going to use horizontal movement learned from this module, but I noticed that if I give my object physics, he doesn’t stay directly on the X-axis, instead kind of shifting back and forth and falling off of my ‘2D’ railing. Any quick fix for this other than doing a big solid back/front panel, or is this not the kind of stuff you want to use to create a 2D platformer with a better view.

    • Hi Nick! its not dead, I still look at stuff, I’m just making something similar and much bigger for Unity directly.

      As for restricting – just use Constraints on the Rigidbody component if you use one, or if its a character controller, then consistently set this back to the X position it should be every so often – try not to do it every frame but you should check if it has drifted by a certain amount and set it back. Remember also if you want true 2D you should use orthographic cameras too. Change the Projection setting on your camera.

    okay, Im trying to right a script so a cube goes 1 unit forward with one push of the up arrow, 1 back with one push of back arrow, and same for sides. Essentially, I want to write a script for box like the one in http://www.coolmath-games.com/0-briker-2/ . Any clue how? (Im new and suck at scripting)

    • in a basic sense you could do it like this –

      function Update () {
      //go right
      //go left
      else if(Input.GetKeyUp("a")){
      //go up
      else if(Input.GetKeyUp("w")){
      //go down
      else if(Input.GetKeyUp("s")){

      • Thnaks, I had to change the y to z, but you helped me alot.

    Any chance of a C# version? 🙂

  • I’ve had a crack at it and it seems to work. Although, I’m not sure if its the most efficient method, so open to suggestions. Also, can anyone confirm that floats cannot be used as parameters of a Vector3 in C# (as in this example, in JavaScript). Thanks
    public Vector3 hValue = new Vector3(0, 0, 0);

    void Update () {

    float horiz = Input.GetAxis(“Horizontal”);


    hValue.x = horiz;


  • Here is the code I used for C#:

    using UnityEngine;
    using System.Collections;

    public class Move : MonoBehaviour
    void Update()
    var horiz = Input.GetAxis(“Horizontal”);
    Vector3 goLeftRight = new Vector3(horiz, 0, 0);



  • CT: The problem is not in your code but somewhere else…
    I spent an hour around this and found that you cannot apply the script to an *EMPTY* GameObject as in this video @1:35. Try applying it to PointLight or MainCamera.

    Is this a bug or just a change in version?
    Unity 4.0.1 & MonoDevelop C#

    Tnx anyway for your great video instructions, Will! They’re perfect in comparasion to others found from Youtube. 🙂

    Here’s the code in C#:

    using UnityEngine;
    using System.Collections;

    public class GetAxis : MonoBehaviour {

    void Update () {

    float horiz = Input.GetAxis(“Horizontal”);
    transform.Translate(horiz, 0, 0);


  • I am having trouble with this. Not with the lesson it self. I am making a simulation for my class, and I made the object I want to control in 3DS Max. When I apply GetAxis to my object it doesn’t move left or right. What am I doing wrong?

    Here is the code I used:

    #pragma strict

    function Start () {


    function Update () {

    var horiz : float = Input.GetAxis(“Horizontal”);


    transform.Translate(Vector3(horiz, 0, 0));


    • Ok I just figured out what I was doing wrong with that code. I have another problem the glider is suppose to bank right, and bank left. Not just move from left and right. How do I get my glider to bank left and right like a real plane?

    Here is the c#:
    using UnityEngine;
    using System.Collections;

    public class HorizontalAxisStrafe : MonoBehaviour {

    void Update () {

    float horiz = Input.GetAxis(“Horizontal”);

    transform.Translate(new Vector3(horiz, 0, 0));

    //had to add new in front of ‘Vector3’


    C# is, almost, like the Java script. all I had to do was add “new” (as shown in the previous post) front of “Vector3”.

  • in c# ? ? ?

    function Update () {
    var horiz : float = Input.GetAxis(“Horizontal”);

  • When I do that it sets value of -1 automatically. What should I do?

  • I have a problem using rigidbody..
    can i ask it here??

  • May i know wht is the value of Time.delatTime??you have multiplied every where but im unable to get its default value…these tutorials are very useful for selflearners…thanku so much

Leave a Comment