FPS_Controller.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace VolumetricFogAndMist2.Demos {
  5. public class FPS_Controller : MonoBehaviour {
  6. // References
  7. CharacterController characterController;
  8. Transform mainCamera;
  9. // Input Internals
  10. float inputHor;
  11. float inputVert;
  12. float mouseHor;
  13. float mouseVert;
  14. float mouseInvertX = 1;
  15. float mouseInvertY = -1;
  16. float camVertAngle;
  17. bool isGrounded = false;
  18. Vector3 jumpDirection = Vector3.zero;
  19. float sprint = 1f;
  20. public float sprintMax = 2f;
  21. public float airControl = 1.5f;
  22. public float jumpHeight = 10;
  23. public float gravity = 20f;
  24. // Character Stats
  25. public float characterHeight = 1.8f;
  26. public float cameraHeight = 1.7f;
  27. public float speed = 15;
  28. // Rotation Vars
  29. public float rotationSpeed = 2;
  30. public float mouseSensitivity = 1;
  31. // Start is called before the first frame update
  32. void Start() {
  33. // Assign refs
  34. characterController = gameObject.AddComponent<CharacterController>();
  35. mainCamera = Camera.main.transform;
  36. // Setup Char
  37. characterController.height = characterHeight;
  38. characterController.center = Vector3.up * characterHeight / 2;
  39. // Setup Cam
  40. mainCamera.position = transform.position + Vector3.up * characterHeight;
  41. mainCamera.rotation = Quaternion.identity;
  42. mainCamera.parent = transform;
  43. // Setup Cursor
  44. Cursor.lockState = CursorLockMode.Locked;
  45. Cursor.visible = false;
  46. }
  47. // Update is called once per frame
  48. void Update() {
  49. Vector3 mousePos = Input.mousePosition;
  50. if (mousePos.x < 0 || mousePos.x >= Screen.width || mousePos.y < 0 || mousePos.y >= Screen.height) return;
  51. isGrounded = characterController.isGrounded;
  52. // Get Input
  53. inputHor = Input.GetAxis("Horizontal");
  54. inputVert = Input.GetAxis("Vertical");
  55. mouseHor = Input.GetAxis("Mouse X");
  56. mouseVert = Input.GetAxis("Mouse Y");
  57. // Rotate player first
  58. transform.Rotate(0, mouseHor * rotationSpeed * mouseSensitivity * mouseInvertX, 0);
  59. // Construct the direction vector
  60. Vector3 moveDirection = transform.forward * inputVert + transform.right * inputHor;
  61. moveDirection *= speed;
  62. if (isGrounded) {
  63. // Increase sprint smoothly
  64. if (Input.GetKey(KeyCode.LeftShift)) {
  65. if (sprint < sprintMax) sprint += 10 * Time.deltaTime;
  66. } else {
  67. if (sprint > 1) sprint -= 10 * Time.deltaTime;
  68. }
  69. if (Input.GetKeyDown(KeyCode.Space)) {
  70. jumpDirection.y = jumpHeight;
  71. } else {
  72. jumpDirection.y = -1;
  73. }
  74. } else {
  75. moveDirection *= airControl;
  76. }
  77. // Apply gravity continuously
  78. jumpDirection.y -= gravity * Time.deltaTime;
  79. // Move the character using Move
  80. characterController.Move(moveDirection * sprint * Time.deltaTime);
  81. characterController.Move(jumpDirection * Time.deltaTime);
  82. // Rotate the camera up and down
  83. camVertAngle += mouseVert * rotationSpeed * mouseSensitivity * mouseInvertY;
  84. // Clamp value so we don't go over ourselves
  85. camVertAngle = Mathf.Clamp(camVertAngle, -85f, 85f);
  86. // Apply Rotation
  87. mainCamera.localEulerAngles = new Vector3(camVertAngle, 0f, 0f);
  88. }
  89. }
  90. }