BaseInputController is an extendable class for building input-specific controllers such as keyboard and touch controllers. This script is not intended to be used directly but instead extended, with an extended script loaded based on what platform and/or inputs the project is currently targeting. Later on, I will provide a generic keyboard input controller that extends BaseInputController. This will be the script you will use most in this framework for handling keyboard input. For now, this script is provided as a reference in case you want to build your own input controllers.

BaseInputController.cs

using UnityEngine;
using System.Collections;

public class BaseInputController : MonoBehaviour {
    
    // directional buttons
    public bool Up;
    public bool Down;
    public bool Left;
    public bool Right;
    
    // fire / action buttons
    public bool Fire1;
    
    // weapon slots
    public bool Slot1;
    public bool Slot2;
    public bool Slot3;
    public bool Slot4;
    public bool Slot5;
    public bool Slot6;
    public bool Slot7;
    public bool Slot8;
    public bool Slot9;
    
    public float vert;
    public float horz;
    public bool shouldRespawn;
    
    public Vector3 TEMPVec3;
    private Vector3 zeroVector = new Vector3(0,0,0);
    
    public virtual void CheckInput () {    
        // override with your own code to deal with input
        horz=Input.GetAxis ("Horizontal");
        vert=Input.GetAxis ("Vertical");
    }
    
    public virtual float GetHorizontal() {
        // returns our cached horizontal input axis value
        return horz;
    }
    
    public virtual float GetVertical() {
        // returns our cached vertical input axis value
        return vert;
    }
    
    public virtual bool GetFire() {
        return Fire1;
    }
    
    public bool GetRespawn() {
        return shouldRespawn;    
    }
    
    public virtual Vector3 GetMovementDirectionVector() {
        // temp vector for movement dir gets set to the value of an otherwise unused vector that always have the value of 0,0,0
        TEMPVec3=zeroVector;
        
        TEMPVec3.x=horz;
        TEMPVec3.y=vert;

        // return the movement vector
        return TEMPVec3;
    }

}

Usage

All input scripts use CheckInput() as their default update function. This is intended to be called by the player class (since enemies generally don’t move to player input). This function just takes the axis input from Unity’s default Input class, which already sets up a horizontal and vertical axis. To return a specific axis, the GetHorizontal() and GetVertical() functions can be used. Both of these are virtual in case the developer wants to add additional information depending on the device.

The script assumes the game will utilize some kind of firing input. The GetFire() function is handy for this.

GetRespawn() returns the bool of the variable shouldRespawn. Not every game will have lives, but most games nowadays have a respawn system (meaning shouldRespawn will always be true), so we provide it here for convenience.

Note that GetFire() and GetRespawn() currently are just empty virtual functions – these should be built upon based on game requirements.

GetMovementDirectionVector() will return a Vector3 type based on the current state of input, which is useful for processing movement without having to check individual horizontal and vertical components.

Up Next: Keyboard_Input

The BaseInputController is great for creating specific input controllers, but since most developers will be working on a device with a keyboard, it is handy to have a default keyboard input controller for testing and possibly production use. Read about Keyboard_Input here: https://www.zesix.com/2015/02/unity-keyboard_input-c/