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

4 Common Risks In Software Development Projects

What are the key features of using Documentum ?

5 Tips To Boost the Performance of Your VS Code Editor

Laptop and desktop computers on desk

Docker Swarm — Part 1

Why a Cloud Archive is the Best Email Archiving Solution for SMBs

How do we Hexaheads know what’s happening in Office365 ?

What are the key features to consider while building an uber like app?

Creating a Web App in Erlang with Chicago Boss and Riak from Scratch

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

Learn to code by Making 2.5 D Space Shooter Game.

Working with Screen Space Reflections in Unity’s HDRP

2.5D Platformer setup: Movement and Collectibles

Add a custom template to Unity for new projects