Appendix: Keyboard Turning

In the 3rd workshop we took the assumption that the player has a dual stick controller for granted. In a real situation, that may not be the case. A small adaptation to the code needs to be made to support multiple input configurations.

PlayerMovement.cs

     // Update is called once per frame
    void Update ()
    {
        // We are going to read the input every frame
        Vector3 vNewInput = new Vector3();
        if(Input.GetJoystickNames().Length > 0)
        {
            vNewInput.Set(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);
        }
        // Use keyboard check if no joystick is connected
        else
        {
            // First the 'y' input
            if(Input.GetKey(KeyCode.Q) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.E))
                vNewInput.y = 1.0f;
            else if(Input.GetKey(KeyCode.Z) || Input.GetKey(KeyCode.X) || Input.GetKey(KeyCode.C))
                vNewInput.y = -1.0f;
            // Then the 'x' input
            if(Input.GetKey(KeyCode.Q) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.Z))
                vNewInput.x = -1.0f;
            else if(Input.GetKey(KeyCode.E) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.C))
                vNewInput.x = 1.0f;
        }
        ...
    }

PlayerAiming.cs

    // Update is called once per frame
    void Update ()
    {
        // We are going to read the input every frame
        Vector3 vNewInput = new Vector3();
        if(Input.GetJoystickNames().Length > 0)
        {
            vNewInput.Set(Input.GetAxis("Horizontal_R"), Input.GetAxis("Vertical_R"), 0.0f);
        }
        // Use keyboard check if no joystick is connected
        else
        {
            // First the 'y' input
            if(Input.GetKey(KeyCode.Keypad7) || Input.GetKey(KeyCode.Keypad8) || Input.GetKey(KeyCode.Keypad9))
                vNewInput.y = -1.0f;
            else if(Input.GetKey(KeyCode.Keypad1) || Input.GetKey(KeyCode.Keypad2) || Input.GetKey(KeyCode.Keypad3))
                vNewInput.y = 1.0f;
            // Then the 'x' input
            if(Input.GetKey(KeyCode.Keypad7) || Input.GetKey(KeyCode.Keypad4) || Input.GetKey(KeyCode.Keypad1))
                vNewInput.x = -1.0f;
            else if(Input.GetKey(KeyCode.Keypad9) || Input.GetKey(KeyCode.Keypad6) || Input.GetKey(KeyCode.Keypad3))
                vNewInput.x = 1.0f;    
        }

        // Only do work if meaningful
        if(vNewInput.sqrMagnitude < 0.1f)
        {
            return;
        }
        ...
    }

PlayerShooting.cs

     // Update is called once per frame
    void Update ()
    {
        // We are going to read the input every frame
        if(Input.GetJoystickNames().Length > 0)
        {
            // Check if our aim stick is in use
            Vector3 vNewInput = new Vector3(Input.GetAxis("Horizontal_R"), Input.GetAxis("Vertical_R"), 0.0f);
            if(vNewInput.sqrMagnitude < 0.1f)
            {
                // Reset the shooting timing if we stop shooting
                m_fAccumulatedFireTime = 0.0f;
                return;
            }
        }
        else
        {
            // Check if any of our aim keys are in use
            bool bShoot = Input.GetKey(KeyCode.Keypad1) || Input.GetKey(KeyCode.Keypad2) || Input.GetKey(KeyCode.Keypad3) ||
                          Input.GetKey(KeyCode.Keypad4) || Input.GetKey(KeyCode.Keypad6) ||
                          Input.GetKey(KeyCode.Keypad7) || Input.GetKey(KeyCode.Keypad8) || Input.GetKey(KeyCode.Keypad9);

            if(!bShoot)
            {
                // Reset the shooting timing if we stop shooting
                m_fAccumulatedFireTime = 0.0f;
                return;
            }
        }
        ...
    }

All three of files were modified to include the following:

 if(Input.GetJoystickNames().Length > 0)

This call to the input system will return a list with the names of the joysticks attached to the system, if at anytime the list is empty it will instead read the keyboard status directly and map the left nine letters and they number pad to be moving and shooting.

Swap the files link here for the ones in workshop 3 to try it out. A demo of this working, and it will switch modes while running if you add or remove joysticks.

Alternatively, you can also bind the numpad keys in project’s input manager like this:

The key to good input

The [ ] square brackets are how you specify keys on the number pad, this will also work. As always, there are multiple ways to solve any problem when making games.