Day 137 — Setting up an FPS Project

Hey and welcome!

Now this one is going to be a bit different, we’re continuing our theme of 3D development but we’re actually now going to turn this into a First Person Shooter project. Although if you prefer you can create a new project and call it along the lines of FPS Game.

Let’s start out by adjusting the camera, if youre creating a new project go ahead and set up your Player object again as a capsule making sure that you remove the collider and replace it with a Character Controller component and then add your Main Camera as a child to this object.

To make things easier go ahead and set your Player’s position to 0, 0, 0 and the Camera to 0, 0, 0 as well before you make it a child. Now adjust your camera’s Y position so that it’s about the head height of your Player capsule, I set mine to 0.72:

Once you’re happy with that it’s time to create an FPSController script to attach to your player and add in this code:

[Header("Controller Info")]
[SerializeField ][Tooltip("How fast can the controller walk?")]
private float _walkSpeed = 3.0f;
[SerializeField][Tooltip("How fast can the controller run?")]
private float _runSpeed = 7.0f;
[SerializeField][Tooltip("Set your gravity multiplier")]
private float _gravity = 1.0f;
[SerializeField][Tooltip("How high can the controller jump?")]
private float _jumpHeight = 15.0f;
private bool _crouching = false;
private CharacterController _controller;
private float _yVelocity = 0.0f;
[Header("Camera Settings")]
[SerializeField][Tooltip("Control the look sensitivty of the camera")]
private float _lookSensitivity = 5.0f;
private Camera _fpsCamera;private void Start()
_controller = GetComponent<CharacterController>(); //assign the reference variable to the component
_fpsCamera = GetComponentInChildren<Camera>();
_initialCameraPos = _fpsCamera.transform.localPosition;
Cursor.lockState = CursorLockMode.Locked;}private void Update()

if (Input.GetKeyDown(KeyCode.Escape))
Cursor.lockState = CursorLockMode.None;
void FPSController()
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 direction = new Vector3(h, 0, v);
Vector3 velocity = direction * _walkSpeed;
if (Input.GetKeyDown(KeyCode.C))
_crouching = !_crouching;
if (_crouching == true)
_controller.height = 2.0f;
_controller.height = 1.0f;
if (Input.GetKey(KeyCode.LeftShift) && _crouching == false)
velocity = direction * _runSpeed;
if (_controller.isGrounded == true)
_yVelocity = _jumpHeight;
_yVelocity -= _gravity;
velocity.y = _yVelocity;
velocity = transform.TransformDirection(velocity);
_controller.Move(velocity * Time.deltaTime);
void CameraController()
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
Vector3 rot = transform.localEulerAngles;
rot.y += mouseX * _lookSensitivity; //add our mouseX movement to the y axis
transform.localRotation = Quaternion.AngleAxis(rot.y, Vector3.up); ////rotate along the y axis by movement amount
Vector3 camRot = _fpsCamera.transform.localEulerAngles; //store the current rotation
camRot.x += -mouseY * _lookSensitivity; //add the mouseY movement to the x axis
_fpsCamera.transform.localRotation = Quaternion.AngleAxis(camRot.x, Vector3.right); //rotate along the x axis by movement amount

One thing you’ll notice right away is that we’re adding Headers and Tooltips to our variables that we’re creating. It does look a little messy in here but it’s worth in when you get the following result in your inspector:

Keeps things tidy for you and your designers if you’re having to define a lot of properties each with varying levels of similarities and differences.

In all actuality you’ll notice that the code here is pretty much identical to the code you were using before except for the camera rotation code which I’ve added in comments this time round to help you get your head around what’s happening in here. You’ll also notice that there’s a crouching mechanic now which takes advantage of altering a new Character Controller property which is height in order to have this effect when we press the C key:

Once you’re happy that this code is working for you go ahead and add the Shoot script to your player now and make sure that you assign your Blood Splat prefab in here and your project should be looking like this:

Be sure to adjust the jump height and speed variables if you’re not happy with them but all in all you should have a pretty successful transition into an FPS project!

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.