This article is a continuation on my previous article that setup the different forces need to move the space ship and now we will map those forces to the mouse and keys.
This script is a simplified version of a script created by Midnite Oil Software LLC. Please check out his channel and video
The mouse is going to control the pitch (x-axis rotations) & yaw (y-axis rotation). We are going to declare 3 variables to accomplish this:
- DeadZoneRadius — a float value that will be the size at the center of the screen where the mouse can be without effecting either the pitch or yaw.
- ScreenCenter — a Vector2 (x value, y value) that will hold the position of the center of screen.
- MousePosition — a Vector3 (x value, y value, z value) that will hold the position of the mouse
In the Start method, we set the screenCenter = new Vector 2 which requires a x value and y value. The x value is going to be the width of the screen divided by 2 and the y value will be the screen height divided by 2.
In the Update method, we are going to continuously set the mousePosition to the current position of the mouse using the Input system. This will return a Vector 3 (x, y, z) with the z always being equal to 0.
The yaw will be the distance from x position of the mouse to the center of screen’s x.
This will return a negative number if the mouse is on the left side of screen and a positive number if it is on the right side of the screen.
This value needs to be normalized, so it will need to return the a value from -1 to 1. We do this by dividing the distance between the mouse x and the center of the screen x by the center of the screen x.
This is now returning a value from -1 (left side of the screen) to 1 (right side of the screen) with 0 being at the center.
For the pitch, instead of using the x value, we use the y value.
The mouse moving towards the top of the screen will return a value of 1 and towards the bottom of the screen will return a value of -1.
The dead zone will provide the player an area at the center of the screen where the mouse can be without effect the pitch or yaw. If there wasn’t this dead zone, the player would have to have the mouse perfectly positioned at the center of the screen to have no movement.
The yaw can return a negative value, so we only want the absolute value of yaw using the Math Function Absolute. IF the absolute value of yaw is greater than the deadZoneRadius THEN set the yawAmount = yaw value ELSE yawAmount = 0.
If you are wondering where the yawAmount please read my previous article mentioned in the intro.
Something for pitch, the only difference is we will invert so when the mouse is at the top of screen it will pitch up and vice-a-versa.
The key inputs will control the roll and thrust of the space ship.
This can be straightforward using the Input system. You can use the method GetAxis and pass in the axis string. Horizontal axis is mapped to the left/right arrow keys and Vertical axis is mapped to the up/down arrow keys. These values will return a value in the range of -1 to 1.
We can directly set the roll amount to the Horizontal axis and the thrust amount to the Vertical axis.
All good, now you can dial in the control sensitivity adjusting force amounts using the sliders in the editor.
Cursor Clean Up
We want to keep the cursor confined to the game screen and we can do this by setting the cursor lock stat to confined.
If you want to hide the cursor, you can set its visiblity to false.