Using controller input

One of the biggest advantages of using Unity as a game engine is the fact that you can support multiple platforms with minimal changes to your base game. In fact, right now, if you plug in an Xbox 360 Controller into your computer, restart Unity, and try to play the game, you'll note that the left-hand side joystick already moves the player, and if you press the Y button, you will jump into the air. However, some of the aspects don't work, so let's get them implemented.

Let's get started by performing the following steps:

  1. The first thing that we're going to need to do is let Unity know that we want to work with some new input. So, to do this, we will need to navigate to Edit | Project Settings | Input.
  2. Once there, we need to add four new axes to our project, the first being a new horizontal axis, so right-click on the Mouse X axis and select Duplicate Array Element.
  3. Extend the newly created Mouse X axes and rename it to 360 Right Horizontal. The controller output is never 100% correct, so we want to change the Dead value to .05 so that any value between -.05 and .05 won't be counted.
  4. Change the Type value to Joystick Axis and the Axis value to 4th axis (Joysticks).
    Using controller input
  5. Do the same for the Mouse Y axis with the name 360 Right Vertical using the 5th axis (Joysticks) option. Take a look at the following screenshot:
    Using controller input
  6. We will also want to have triggers work, so add in 360 Left Trigger and 360 Right Trigger by changing the Size of the Axes to 22 and renaming the last 2. Removing the positive and negative buttons. Changing the Type to Joystick Axis and then the Axis to 9 and 10, respectively.
    Using controller input
  7. Now, we will need to alter the character controller's MouseLook script file, so double-click on it to open MonoDevelop. Once opened, add the following bolded code to its LookRotation function:
            public void LookRotation(Transform character, Transform camera)
            {
                float yRot = CrossPlatformInputManager.GetAxis("Mouse X") * XSensitivity;
                float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * YSensitivity;
    
                
                float xRot360 = Input.GetAxis("360 Right Horizontal") * 15 * XSensitivity;
                float yRot360 = Input.GetAxis("360 Right Vertical") * 15 * YSensitivity;
    
                yRot += xRot360;
                xRot -= yRot360;
    
                m_CharacterTargetRot *= Quaternion.Euler (0f, yRot, 0f);
                m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);
    
                if(clampVerticalRotation)
                    m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot);
    
                if(smooth)
                {
                    character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot,
                        smoothTime * Time.deltaTime);
                    camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot,
                        smoothTime * Time.deltaTime);
                }
                else
                {
                    character.localRotation = m_CharacterTargetRot;
                    camera.localRotation = m_CameraTargetRot;
                }
    
                UpdateCursorLock();
            }

    This code will make the right joystick rotate the player's camera.

  8. Now that we have that done, let's get the camera and shoot to work. To do that, we need to now open the PhoneBehaviour script. First of all, we're going to need to introduce a new variable to account for the fact that we want the player to have to release the right trigger before they can shoot again:
    private bool shotStarted = false;
  9. Now, we will update the Update function to the following code. Note the changes set in bold:
        void Update () {
            // Are we holding down the right mouse button
            if ((Input.GetMouseButton(1) || (Input.GetAxis("360 Left Trigger") > 0)) && !cameraActive)
            {
                SetCameraActive(true);
            }
            else if(cameraActive && !(Input.GetMouseButton(1) || (Input.GetAxis("360 Left Trigger") > 0)))
            {
                SetCameraActive(false);
            }
    
            if (cameraActive && (Input.GetMouseButton(0) || (Input.GetAxis("360 Right Trigger") > 0)))
            {
                shotStarted = true;
    
                StartCoroutine(CameraFlash());
    
                GameObject[] enemyList = GameObject.FindGameObjectsWithTag("Enemy");
    
                foreach (GameObject enemy in enemyList)
                {
                    if (enemy.activeInHierarchy)
                    {
                        EnemyBehaviour behaviour = enemy.GetComponent<EnemyBehaviour>();
                        behaviour.TakeDamage();
                    }
                }
    
            }
    
            if (Input.GetAxis("360 Right Trigger") == 0)
            {
                shotStarted = false;
            }
    
    
        }
  10. Save your scripts and start the game. Take a look at the following screenshot:
    Using controller input

At this point, you should be able to play the project using an Xbox 360 Controller!

Now, depending on what platform you're running Unity, there may be special things to take into consideration. For more information about using the Xbox 360 Controller, visit http://wiki.unity3d.com/index.php?title=Xbox360Controller.

Note

For those who don't want to deal with input too much and just want something that will standardize your input for common controllers, I hear good things about Gallant Games' InControl input manager. You can find out more about from http://www.gallantgames.com/incontrol.

While not all the features are included, they have an open source version of most of the content at http://github.com/pbhogan/InControl, but if you use their tool and find it useful, I recommend that you buy it to support further development.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset