Building a First-Person Shooter: Part 1.4 Mouse Inputs
In this tutorial we will add in mouse movements to allow the player to look around the room.
sensitivity=1.0; cameralooksmoothing = 2.0; cameraypositionsmoothing = 3.0; smoothedcamerapositiony = 0; In UpdateControls we will need access to the games context which is the renderable area of the window, which is essentially the area of the game window inside the windows border. We use this context to find the center of the screen and store the coordinates as sx and sy. Context* context = Context::GetCurrent(); //Get the mouse movement float sx = context->GetWidth()/2; float sy = context->GetHeight()/2; Next we save the current mouse position and then return the mouse to the center of the screen: //Get the mouse position Vec3 mouseposition = window->GetMousePosition(); //Move the mouse to the center of the screen window->SetMousePosition(sx,sy); Now that we know where the center of the screen is and the current mouse position we can figure out the difference between the two which tells us which direction to look: //Get change in mouse position float dx = mouseposition.x - sx; float dy = mouseposition.y - sy; We want to set the mouse speed by smoothing between the previous mouse speed and the distance from the center of the screen. //Mouse smoothing mousespeed.x = Math::Curve(dx,mousespeed.x,cameralooksmoothing/Time::GetSpeed()); mousespeed.y = Math::Curve(dy,mousespeed.y,cameralooksmoothing/Time::GetSpeed()); Using the mouse speed we increment the player's rotation and scale it by sensitivity, thus the higher the sensitivity the quicker the mouse movements: //Adjust and set the camera rotation playerrotation.x += mousespeed.y*sensitivity / 10.0; playerrotation.y += mousespeed.x*sensitivity / 10.0; To prevent the player from inhuman neck movements we clamp the y rotation values at -90 and 90: //Prevent inhuman looking angles playerrotation.x = Math::Clamp(playerrotation.x,-90,90); In the player Update function we rotate the camera by the playerrotation value finally allowing for our character to look around: //Set camera rotation camera->SetRotation(playerrotation,true); At this point the player character can move around with keyboard inputs and look around using the mouse: #include "MyGame.h" using namespace Leadwerks; Player::Player() { //Create the entity entity = Pivot::Create(); entity->SetUserData(this); //Initialize values sensitivity=1.0; cameralooksmoothing = 2.0; move = 0.0; strafe = 0.0; cameraypositionsmoothing = 3.0; maxacceleleration = 0.5; movementspeed = 3.0; standheight=1.7; crouchheight=1.2; cameraheight = standheight; smoothedcamerapositiony = 0; //Create the player camera camera = Camera::Create(); camera->SetPosition(0,entity->GetPosition().y + cameraheight,0,true); //Set up player physics entity->SetPhysicsMode(Entity::CharacterPhysics); entity->SetCollisionType(Collision::Character); entity->SetMass(10.0); //Player position entity->SetPosition(0,0,0,true); } Player::~Player() { if (camera) { camera->Release(); camera = NULL; } } void Player::UpdateControls() { Window* window = Window::GetCurrent(); Context* context = Context::GetCurrent(); //Get inputs from the controller class move = window->KeyDown(Key::W) - window->KeyDown(Key::S); strafe = window->KeyDown(Key::D) - window->KeyDown(Key::A); //Get the mouse movement float sx = context->GetWidth()/2; float sy = context->GetHeight()/2; //Get the mouse position Vec3 mouseposition = window->GetMousePosition(); //Move the mouse to the center of the screen window->SetMousePosition(sx,sy); //Get change in mouse position float dx = mouseposition.x - sx; float dy = mouseposition.y - sy; //Mouse smoothing mousespeed.x = Math::Curve(dx,mousespeed.x,cameralooksmoothing/Time::GetSpeed()); mousespeed.y = Math::Curve(dy,mousespeed.y,cameralooksmoothing/Time::GetSpeed()); //Adjust and set the camera rotation playerrotation.x += mousespeed.y*sensitivity / 10.0; playerrotation.y += mousespeed.x*sensitivity / 10.0; //Prevent inhuman looking angles playerrotation.x = Math::Clamp(playerrotation.x,-90,90); } //Update function void Player::Update() { UpdateControls(); float maxaccel = this->maxacceleleration; float movespeed = this->movementspeed; //Make sure movements are normalized so that moving forward at the same time as strafing doesn't move your character faster normalizedmovement.z = move; normalizedmovement.x = strafe; normalizedmovement = normalizedmovement.Normalize() * movespeed; //Set camera rotation camera->SetRotation(playerrotation,true); entity->SetInput(playerrotation.y,normalizedmovement.z,normalizedmovement.x,0,false,maxaccel); //Set the camera position cameraposition = entity->GetPosition(); camera->SetPosition(cameraposition.x, cameraposition.y + cameraheight, cameraposition.z ); }
Related Tutorials
Balancing Game Development and Creative Direction in Indie Production
A practical look at how indie developers can balance creative direction with hands-on game development. This article co…
My Unreal Engine Development Process: From Core Idea to Playable Build
A practical overview of my Unreal Engine development process, covering how I move from a core game idea to a playable b…
Introducing LaneGraph: The Ultimate Road Network Solution for Unity
Discover the power of LaneGraph, a lightweight and flexible lane-based navigation system for Unity. LaneGraph makes it…
Retargeting Mixamo Characters with Root Motion In Unreal Engine 5.4.
I have always found Retargeting Mixamo Characters To have Root Motion is a serious lengthy Tast, Recently I stumbled up…
How To Make A SIMPLE Main Menu In Unity
In this tutorial for unity, i go over how to make a simple main menu for unity, it's an unlisted video because i do not…
Guide to Gameplay Balance
A perspective on competitive gameplay balance, from a background of "shooter" sandbox design.
Discussion
More from Christopher Vossen
Building a First-Person Shooter Part 1.2: The Player Class
In this tutorial we will begin to flesh out our player class by adding in a camera and setting up character physics.
Building a First-Person Shooter Part 1.1: Visual Studio Setup
This is the second lesson in a set of tutorials that demonstrate how to build a complete first-person shooter game, fro…
Building a First-Person Shooter: Part 1.6 Sound
In this tutorial we will finish off this First-Person Shooter Tutorial 1 by adding in sound effects into the game.
Discussion