This is a basic keyboard input class to be customized for a particular game’s needs and loaded depending on device.


using UnityEngine;
using System.Collections;

public class Keyboard_Input : BaseInputController {
    public override void CheckInput () {    
        // get input data from vertical and horizontal axis and store them internally in vert and horz so we don't
        // have to access them every time we need to relay input data.
        vert = Input.GetAxis ("Vertical");
        horz = Input.GetAxis ("Horizontal");
        // set up some boolean values for up, down, left and right
        Up      = (vert > 0);
        Down    = (vert < 0);
        Left    = (horz < 0);
        Right   = (horz > 0);    
        // get fire / action keys
        Fire1 = Input.GetButton ("Fire1");
        shouldRespawn = Input.GetButton ("Fire3");
    public void LateUpdate () {
        // check inputs each LateUpdate() ready for the next tick
        CheckInput ();

The reason why we check for input in LateUpdate() is to ensure all physics handling to have resolved by the time we handle new input. This is particularly important when physics may impact the position of the camera and player object.

Up Next: Mouse_Input

Along the same lines as keyboard input, most developers coding on desktops will have access to a mouse and choose to use it as part of the game’s desktop controls if that is the target platform. Find the Mouse_Input script here: