-
Notifications
You must be signed in to change notification settings - Fork 8
unhandled_input()
Here we take care of mouse movement. The reason we don’t take care of any other input is because I can’t figure out how. I’ve already tried looking online and experimenting but to no avail. So for now I just do other inputs within other functions, like the “_physics_process()” function.
Unhandled input, and also the “_input()” function, only activate when there is actual input. So that means that if the player isn’t moving a mouse or pressing a key, this function isn’t executed.
The function line looks like this:
func _unhandled_input(event):
“event” is the event that we are going to be checking. It can be many types of events, be it key presses, mouse movement, or joystick movement.
The first lines:
if(event is InputEventMouseMotion):
if(MouseLook):
First it checks to see if the current input event is mouse motion. Then it checks to see if the “MouseLook” variable is true, allowing the player to look around with the mouse.
Mouse_Rel_Movement = event.relative
This gets the relative mouse movement in a 2D vector. Relative mouse movement is how many pixels, in floating point format, that the mouse cursor has moved from the last frame. It’s best used with the mouse cursor being captured by the program, naturally.
So if you, say, move the mouse 1 pixel to the left and 10 pixels up, the vector would look like this:
Mouse_Rel_Movement = Vector2( -1.0 , -10.0 )
Then you use that to find out how much to turn the player.
Cam_Local_Rot_X = rad2deg(Node_Camera3D.get_rotation().x)
This gets the local rotation of the camera on the X axis. Now, this can be confusing because when you think of “X axis” you may think of left and right. That’s correct. But rotation on the X axis means looking up and down. Understand? Here are some pictures to help:
The above picture shows the camera moving on the x axis.
And this picture shows the camera rotating on the x axis.
Cam_Temp_XRot_Var = Cam_Local_Rot_X + -Mouse_Rel_Movement.y
Now that we have the X axis rotation of the camera, now we will calculate the new rotation of the camera. We do that by adding the negated relative Y mouse movement to the X axis rotation value of the camera we just got. This will be a temporary variable.
Now we need to check that temporary X axis rotation to see if the player’s camera is past looking straight up or down.
if(Cam_Temp_XRot_Var < -90):
Final_Cam_Rot_Local_X = Cam_Temp_XRot_Var + 90 +
Mouse_Rel_Movement.y
This "if" statement checks if the temporary rotation variable is too far down, past looking straight down. If it is we set the rotation to be applied to camera to be as much as it takes to make the camera look straight down.
elif(Cam_Temp_XRot_Var > 90):
Final_Cam_Rot_Local_X = Cam_Temp_XRot_Var - 90 + Mouse_Rel_Movement.y
On the other hand, if the camera is looking too far up, we do the same thing with this but in the opposite direction.
else:
Final_Cam_Rot_Local_X = Mouse_Rel_Movement.y
And if the camera X rotation isn’t going past the upper or lower bounds, we simply make the final rotation to be applied to the camera the same number as the mouse’s relative Y axis movement.
Mouse_Moved = true
And then we set “Mouse_Moved” to true, so that when it comes time to rotate the camera and the player, it will do so. But this isn’t done until the “_physics_process() > FINAL MOVEMENT APPLICATION” section.