User Input

Big Ideas:

  • Unity has a built in Input Manager, that has virtual "buttons" and virtual "axes". These are dynamic and can be tied into basically any input device.
  • Sense the input in the Update(), because this gets called every frame
  • Apply forces based on the input in the FixedUpdate(), as this is called at a fixed time rate that is not affected by frame rate.

Useful Commands

  • Input.GetButtonDown(string buttonName) - This senses whether the button was depressed this frame. It is false while button is being held down. This is useful for one-time events like jumping and firing. Returns true or false.
  • Input.GetButton(string buttonName) - This senses whether the button is being held down. True the first frame and every frame it is being held/pressed. This is useful for ongoing events like a gas pedal, using a jetpack, blocking in a fighting game, etc. Returns true or false.


  • Input.GetAxis(string axisName) - Returns a float between -1 and +1, with a full gradient of values between them. This is useful for sensing a joystick type motion.
  • Input.GetAxisRaw(string axisName) - Returns only -1, 0, or +1. This is the same as the previous except without the in between values. Think of the last one as a joystick with gradual positions, and this as the D-Pad where motion is either: left is being pressed, nothing is being pressed, or right is being pressed.


Mouse and Touchscreen

I will make separate posts about these, but here are the few most useful ones.

  • Input.GetMouseButtonDown(int buttonNumber) - Gets the one time event of the mouse button being pressed, just like "GetButtonDown()". Zero is the left mouse button, and you can get all of the values of your wheel and right mouse by playing around. Returns true or false.
  • Input.GetMouseButton(int buttonNumber) - Same as "GetButton()" but with the mouse. Detects ongoing if the button is being held down. Returns true or false.
  • Input.mousePosition - Returns the position of the mouse in screen pixels. You then need to use the current camera in your Unity scene to figure out where that screen pixel location is in your virtual world. This is done by shooting a ray into the gameworld from the screen pixel location on the camera in the direction the camera is facing, then seeing what the ray hits. The depth of this ray in a 3D game can be sticky, but is very simple in 2D.


  • Input.touchCount - Returns an integer number of touches detected during the last Update() call. This is the common IF statement before using the touches to do something. You could write: "if (Input.touchCount>0)" ... then use the first touch ...
  • Input.GetTouch(int numberOfTouch) - Returns a struct of type "Touch" that holds all of the relevant information such as position, how long it has been held, how hard it is being pressed, etc.. Examples and more info can be found on Unity's documentation: https://docs.unity3d.com/ScriptReference/Input.GetTouch.html

These slides have a lot of information on how to set up the Unity Input Manager

05.0 2D Game Character Movements in Unity (App and Game Design)

Code Snippets (using Forces - not constant speed)

Basic example of how to get and use left/right input on your player's Rigidbody (no speed variable, so no control over left/right force).


Example of left/right input with a public Speed variable


Example of a more complete, yet basic first person controller. This includes:


Code Snippets (constant speed)

Basic example of how to use left/right controls to travel at a constant speed

(coming soon)


Other Great Resources

Videos by Brackeys

  1. "2D Movement In Unity (Tutorial)" by Brackeys - A 21min video from July 2018 that walks you through much of what is in the tutorial above, and more. Brackeys is awesome. This is specifically focused on movement for a side-scroller type game, not top-down.
  2. "Top Down Movement in Unity" by Brackeys - A 22min video from August 2019 that focuses on a top-down game's player controls and movement. Brackeys is awesome.
  3. "Controller Input in Unity" by Brackeys - A 16min video from July 2019 that focuses on using a handheld game controller such as a PS4 controller or XBox controller. These can be connected to your PC by Bluetooth or USB, or connected to a gaming console that your game is loaded onto. This video shows how Unity handles this and how you can code with this input.
  4. "Touch Controls in Unity" by Brackeys - A 16min video from Sept 2018 that focuses on using touch controls with Unity. For example, if you run your program on an iPad or Android phone, how does Unity handle the player touching the screen?


Videos by BlackThornProd

  1. "How to Make a 2D Character Controller in Unity - Easy Tutorial" by BlackThornProd - A 7min video from Jan 2018 by one of my favorite YouTubers. It goes over how to move your player at a constant speed using the rigbody instead of using an AddForce.

Great Complete Series on First Person Controller

  1. "2D Character Animation in Unity [part 1 of 4]" (10min, Sep 2019) by PressStart - A brief video walking you through setting up basic animations for your 2D character.
  2. "Super Mario - Running Tutorial in Unity [part 2 of 4]" (12min, Oct 2019) by PressStart- A follow up video that builds on the previous video. This video goes over smooth left and right motion controls for your 2D character.
  3. "A Perfect Jump in Unity - A Complete Guide [part 3 of 4]" (17min, Oct 2019) by PressStart - A follow up video that builds on the previous video. This goes over a basic jump after testing if you are grounded, then tweaks it to be more responsive and gamer-friendly. Great tips in here!
  4. Wall Jump video TBA


Unity Official Tutorials and Videos

  1. "Beginner 2D UFO Game 3 of 9: Controlling the Player - Unity Official Tutorials" by Unity - A 17min video from Jan 2016 that goes over all of the basics of getting and using basic 2D player input for movement.
  2. "Recorded Video Session: 2D Platformer Character Controller" by Unity - This is a 70min webpage tutorial with YouTube videos of recorded livestream as well as the code snippets involved. This is if you want to take a much deeper dive into 2D player controls.


Other Useful Videos and Tutorials

  1. "Unity Movement [RigidBody vs Translate]" by Press Start - A useful 9min video from Oct 2018 that looks at different ways to move, such as transform.Translate(), rb.MovePosition() and more.
  2. "Unity 2D Movement Tutorial - Script C#" by Xlaugts - A dry 5min video from May 2018 that uses rigidbody.velocity to travel at a constant speed based on user input.