Language:
Page Info
Skill Level:
Engine Version:

2.4 - Implementing Mouse Camera Control

During this step, we're going to add the ability for our character to look around and steer with the mouse.

Turn Axis Mapping

  1. In the Edit menu, click on Project Settings.

  2. Under the Engine heading on the left side of the Project Settings tab, click on Input.

  3. Under Bindings, click on the plus sign next to Axis Mappings.

  4. Click on the arrow to the left of Axis Mappings.

  5. Type "Turn" into the text field that appears, then click on the arrow to the left of the text box to expand the axis binding options.

  6. In the dropdown menu, select Mouse X from the Mouse dropdown list.

  7. Your input settings should now look like the following:

    TurnAxisMapping_MouseX.png

Look-up Axis Mapping

  1. Under Bindings, click on the plus sign next to Axis Mappings.

  2. Type "LookUp" into the text field that appears, then click on the arrow to the left of the text box to expand the axis binding options.

  3. In the dropdown menu, select Mouse Y from the Mouse dropdown list.

  4. Type "-1.0" in the Scale field next to Mouse Y.

  5. Your input settings should now look like the following:

    TurnAxisMapping_MouseY.png

  6. Close the Project Settings menu.

Implementing Input Handling

Now it's time to add code to handle mouse input for turning and looking up. The Character base class defines the two necessary functions for us, namely:

  • AddControllerYawInput

  • AddControllerPitchInput

If you want to perform additional processing, such as adding support for sensitivity or axis inversion, you could provide your own functions to adjust the input values before passing them to function; however, in this case, you'll bind your inputs directly to the AddControllerYawInput and AddControllerPitchInput functions.

  1. Add the following lines to the SetupPlayerInputComponent in FPSCharacter.cpp.

    // Set up "look" bindings.
    PlayerInputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);
    PlayerInputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);
  2. The SetupPlayerInputComponent function should now look like the following:

    // Called to bind functionality to input
    void AFPSCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
    {
        Super::SetupPlayerInputComponent(PlayerInputComponent);
    
        // Set up "movement" bindings.
        PlayerInputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
        PlayerInputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);
    
        // Set up "look" bindings.
        PlayerInputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);
        PlayerInputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);
    }

Testing Mouse Camera Control

  1. Save the FPSCharacter implementation file in Visual Studio.

  2. Locate FPSProject in the Solution Explorer.

  3. Right-click on FPSProject and select Build to compile your project.

    BuildProject.png

  4. After the build finishes, open your FPSProject in Unreal Editor.

  5. Click the Play button in the Level Editor Toolbar. You can now control the camera with your mouse.

  6. Press the Escape key or click the Stop button in the Level Editor to exit Play in Editor (PIE) mode.