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:
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.360 Right Vertical
using the 5th axis (Joysticks) option. Take a look at the following screenshot: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.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.
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;
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; } }
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.
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.