<strong>This Tutorial has been created using Unreal Engine 4.8.2</strong>.
Make sure you are working on the same version of the engine.
Let’s start by writing down look around controls requirements: – Player can look around using mouse on PC and touch screen on mobile device. Additionally on mobile he can use accelerometer too, – Player should be able to change sensitivity for mouse, touch screen and accelerometer,
IMPORTANT NOTE: Tilt will work only for iOS devices! If you are creating Android game you should think twice if you want to support Tilt. Basically there is a lot of android hardware our there and supporting them all is really hard if you don’t have the devices and programmers. Tilt will work different on different Devices because of different tilt hardware.
CREATING NEEDED DATA
- ENUM: ControllingDevice with Mouse, Touch and Gyro added,
- Game Instance: ShooterGameInstance, (we had created it earlier)
- GameplayPlayerController, GameplayPlayerCharacter, GameplayGameMode, GameplayGameState, GameplayGameHUD (we had created them earlier)
IN GAMEPLAY PLAYER CONTROLLER This class will be responsible for look around controls. Open it and add some variables:
- CurrentControlingDevice (ENUM ControllingDevice) default should be set to Mouse,
- MouseSensitivityMin, float (default: 0.1)
- MouseSensitivityMax, float (default: 2)
- MouseSensitivityCurrent, float (default: 1)
- TouchSensitivityMin, float (default: 15)
- TouchSensitivityMax, float (default: 5)
- TouchSensitivityCurrent, float (default: 10)
- GyroSensitivityMin, float (default: 20)
- GyroSensitivityMax, float (default: 60)
- GyroSensitivityCurrent, float (default: 40)
Now add couple of functions.
- GetCurrentControllingDevice. This should be pure function.
- GetSensitivity (add local variable LocalSensitivity)
USEFUL TIP: If you will know that variable will be changed from other class ALWAYS put a function like SetSensitivity to change that variable. Never set variables from other classes different than class in which variable was created! Use functions instead. Like I did with SetSensitivity and SetCurrentControllingDevice. The rule is that only class which have variable declared can change it.
We are still in GameplayPlayerController but we need to add new input axis in Project Settings. To do so go to Project Settings -> Input and add two new axis from MouseX and MouseY (MouseY should have scale “-1”) as in screen.
MOUSE LOOK AROUND Now when you go to GameplayPlayerController you can find your MouseX and MouseY inputs. Let’s create events for them to drive look around for mouse.
From now controls are done for mouse, you can test it out!
USEFUL TIP: To test Touch or Gyro controls you can use UDKRemoteTool. Just run the game on PC, run UDKRemoteTool and set your PC IP. Your device need to be in the same network as your PC.
It’s all for the controlling part. It was easy! Now let’s move to changing sensitivity with UMG widget.
CHANGE SENSITIVITY IN UMG I will use UMG to change sensitivity, but it will be only debug widget. UMG is great tool and I will do a lot of tutorials when doing final UI. For now it will be just debug panel to change sensitivity. First you need to create new Widget. Name it UI_Debug_ChangeSensitivity.
Now go to graph and create function GetController. It should be pure and return GameplayPlayerController. You should create such events early in Blueprints because they will look much more cleaner later.
Now in event graph create event Construct and update sliders information. I will use Float Map Range which is super cool for this kind of things.
Now if you click on the Slider in Designer view you can add new event – OnValueChanged.
Add this event for all of the sliders. We are doing the same thing so here’s screen with all of them added.
This is all in the widget. As I said it’s only for debug. UMG is big and powerful tool and I will definitely do tutorials about it.
LAUNCHING UI_Debug_UI_Debug_ChangeSensitivity Go back to GameplayPlayerController and create a key event. Right mouse on graph and just type “O” you will see key events.
That’s all in this post. You have mouse touch and tilt controls implemented and you can change sensitivity for all of them using custom widget. Hopefully this post will be useful for you guys!