Skip to content

19 Gamepad or Mouse

Russ Painter edited this page Feb 2, 2019 · 2 revisions

Playing it with the keyboard, the movements are jerky. It would be smoother to play with a gamepad or mouse. Lets add both!

Moving

If you plug in a gamepad, you'll see it already mostly works. Since we used the Horizontal and Vertical inputs, unity by default maps these to the normal movement keys and the gamepad. So we just need to add mouse movement.

  1. Open the PlayerMove script attached to the Player prefab and change the GetInput() function so that it adds the mouse movements to the gamepad/keyboard movements.

        input.x = Input.GetAxis("Horizontal") + Input.GetAxis("Mouse X");
        input.y = Input.GetAxis("Vertical") + Input.GetAxis("Mouse Y");
  2. While we're here, in the Start() function let's hide the mouse cursor with

       Cursor.visible = false;
  3. This works, but the movement isn't smooth enough. Open the Project Settings window and select the Input tab. For bot Horizontal and Vertical change Gravity to 2 and Sensitivity to 2

Shooting

  1. Open the PlayerShoot script attached to the Player prefab's Gun object.

  2. In the line where it's checking for the space bar being pressed, add a check for the Fire1 button. The || operator in C# means OR.

         if (Input.GetKeyDown(KeyCode.Space) || Input.GetButtonDown("Fire1"))

    The Fire1 input is already mapped to the gamepad's A button by default.

Clone this wiki locally