Day 137 — Setting up an FPS Project

[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;
[SerializeField]
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()
{
FPSController();
CameraController();

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;
}
else
{
_controller.height = 1.0f;
}
}
if (Input.GetKey(KeyCode.LeftShift) && _crouching == false)
{
velocity = direction * _runSpeed;
}
if (_controller.isGrounded == true)
{
if(Input.GetKeyDown(KeyCode.Space))
{
_yVelocity = _jumpHeight;
}
}
else
{
_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
}

--

--

--

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.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Fixing the Blank Screen issue with OBS using Nvidia Graphics Card

Introducing App Analytics in the Uber Developer Dashboard

Access Google Secret Manager from Google Kubernetes Engine without Service Account Key

Wondershare Recoverit Ultimate v10.0.8.10 for MacOS

5 reasons why SQL and Excel are your BFFs

Encode x Polygon Hackathon: Announcing Covalent

TryHackMe — Alfred

How to set and update cell contents in an Excel XLSX by index in Python

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Connor Fullarton

Connor Fullarton

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.

More from Medium

Quick and Easy: Alter Your Pre-Built Unity Texture Maps using Krita.

My first game in Unity

Creating Enemy Explosions In Unity

Creating Fog and Using Particles in Unity3D and the URP