NovaCode

Basic FPS Camera

Dec 14th, 2025
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | Source Code | 0 0
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(CharacterController))]
  4. public class FPSCamera : MonoBehaviour
  5. {
  6.     [Header("Movement")]
  7.     public float moveSpeed = 5f;
  8.     public float sprintSpeed = 8f;
  9.     public float gravity = -9.81f;
  10.  
  11.     [Header("Mouse Look")]
  12.     public float mouseSensitivity = 200f;
  13.     public Transform cameraHolder;
  14.     public float lookSmooth = 10f;
  15.  
  16.     [Header("Camera Effects")]
  17.     public float headBobAmount = 0.05f;
  18.     public float headBobSpeed = 10f;
  19.     public float swayAmount = 2f;
  20.     public float normalFOV = 60f;
  21.     public float sprintFOV = 70f;
  22.     public float fovSmooth = 8f;
  23.  
  24.     CharacterController controller;
  25.     Camera cam;
  26.     float xRotation;
  27.     float headBobTimer;
  28.     Vector3 velocity;
  29.  
  30.     void Start()
  31.     {
  32.         controller = GetComponent<CharacterController>();
  33.         cam = cameraHolder.GetComponentInChildren<Camera>();
  34.         Cursor.lockState = CursorLockMode.Locked;
  35.     }
  36.  
  37.     void Update()
  38.     {
  39.         HandleMouseLook();
  40.         HandleMovement();
  41.         HandleCameraEffects();
  42.     }
  43.  
  44.     void HandleMouseLook()
  45.     {
  46.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
  47.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
  48.  
  49.         xRotation -= mouseY;
  50.         xRotation = Mathf.Clamp(xRotation, -80f, 80f);
  51.  
  52.         cameraHolder.localRotation = Quaternion.Lerp(
  53.             cameraHolder.localRotation,
  54.             Quaternion.Euler(xRotation, 0f, 0f),
  55.             lookSmooth * Time.deltaTime
  56.         );
  57.  
  58.         transform.Rotate(Vector3.up * mouseX);
  59.     }
  60.  
  61.     void HandleMovement()
  62.     {
  63.         float speed = Input.GetKey(KeyCode.LeftShift) ? sprintSpeed : moveSpeed;
  64.  
  65.         float x = Input.GetAxis("Horizontal");
  66.         float z = Input.GetAxis("Vertical");
  67.  
  68.         Vector3 move = transform.right * x + transform.forward * z;
  69.         controller.Move(move * speed * Time.deltaTime);
  70.  
  71.         if (controller.isGrounded && velocity.y < 0)
  72.             velocity.y = -2f;
  73.  
  74.         velocity.y += gravity * Time.deltaTime;
  75.         controller.Move(velocity * Time.deltaTime);
  76.     }
  77.  
  78.     void HandleCameraEffects()
  79.     {
  80.         // Head bob
  81.         if (controller.velocity.magnitude > 0.1f && controller.isGrounded)
  82.         {
  83.             headBobTimer += Time.deltaTime * headBobSpeed;
  84.             float bob = Mathf.Sin(headBobTimer) * headBobAmount;
  85.             cameraHolder.localPosition = Vector3.Lerp(
  86.                 cameraHolder.localPosition,
  87.                 new Vector3(0, bob, 0),
  88.                 Time.deltaTime * lookSmooth
  89.             );
  90.         }
  91.  
  92.         // Camera sway
  93.         float swayX = -Input.GetAxis("Mouse X") * swayAmount;
  94.         float swayY = -Input.GetAxis("Mouse Y") * swayAmount;
  95.  
  96.         Quaternion swayRotation = Quaternion.Euler(swayY, swayX, 0);
  97.         cameraHolder.localRotation *= swayRotation;
  98.  
  99.         // FOV kick
  100.         float targetFOV = Input.GetKey(KeyCode.LeftShift) ? sprintFOV : normalFOV;
  101.         cam.fieldOfView = Mathf.Lerp(
  102.             cam.fieldOfView,
  103.             targetFOV,
  104.             Time.deltaTime * fovSmooth
  105.         );
  106.     }
  107. }
  108.  
Tags: Unity
Advertisement
Add Comment
Please, Sign In to add comment