Advertisement
Guest User

code

a guest
Jun 2nd, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.79 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Diagnostics;
  4.  
  5. [RequireComponent(typeof(CharacterController))]
  6. public class FPSController : MonoBehaviour
  7. {
  8. [Header("Look Settings")]
  9. public Transform playerCamera;
  10. public float mouseSensitivity = 100f;
  11. public float maxLookAngle = 90f;
  12. public Transform cameraPivot;
  13.  
  14. [Header("Movement Settings")]
  15. public float walkSpeed = 5f;
  16. public float runSpeed = 8f;
  17. public float crouchSpeed = 3f;
  18. public float jumpHeight = 1.5f;
  19. public float gravity = -9.81f;
  20.  
  21. [Header("Keybinds")]
  22. public KeyCode runKey = KeyCode.LeftShift;
  23. public KeyCode crouchKey = KeyCode.LeftControl;
  24.  
  25. [Header("Kick Settings")]
  26. public float kickRange = 2f;
  27. public float kickForce = 500f;
  28. public float kickCooldown = 1f;
  29. public LayerMask kickableLayers;
  30.  
  31. private bool isKicking = false;
  32. private float kickTimer = 0f;
  33.  
  34. [Header("Slide Settings")]
  35. public float slideSpeed = 10f;
  36. public float slideDuration = 1f;
  37. public float slideCooldown = 1.5f;
  38. public float slideHeight = 1f;
  39.  
  40. [Header("Crouch/Slide Shared Settings")]
  41. public float crouchHeight = 1.2f;
  42. public float crouchCameraHeight = 0.8f;
  43. public float cameraSmoothSpeed = 8f;
  44.  
  45. [Header("Model Settings")]
  46. public Transform playerModel;
  47. public float modelYWhenStanding = 0f;
  48. public float modelYWhenCrouching = -0.5f;
  49. public float modelYWhenSliding = -0.7f;
  50.  
  51. [Header("Ground Check Settings")]
  52. public float groundCheckDistance = 0.4f;
  53. public float groundCheckRadius = 0.3f;
  54. public LayerMask groundMask;
  55.  
  56. private CharacterController controller;
  57. private Animator animator;
  58.  
  59. private float cameraPitch;
  60. private float originalHeight;
  61. private Vector3 originalCenter;
  62. private float originalCameraY;
  63. private float targetCameraY;
  64. private bool isPointing = false;
  65.  
  66. private Vector3 velocity;
  67. private bool isSliding = false;
  68. private bool isCrouching = false;
  69. private float slideCooldownTimer = 0f;
  70. private bool isGrounded = false;
  71.  
  72. void Start()
  73. {
  74. controller = GetComponent<CharacterController>();
  75. animator = GetComponentInChildren<Animator>();
  76. animator.cullingMode = AnimatorCullingMode.CullUpdateTransforms;
  77.  
  78. Cursor.lockState = CursorLockMode.Locked;
  79. Cursor.visible = false;
  80.  
  81. cameraPitch = 0f;
  82. playerCamera.localRotation = Quaternion.Euler(cameraPitch, 0f, 0f);
  83.  
  84. originalHeight = controller.height;
  85. originalCenter = controller.center;
  86. originalCameraY = playerCamera.localPosition.y;
  87. targetCameraY = originalCameraY;
  88.  
  89. if (playerCamera == null)
  90. UnityEngine.Debug.LogError("PlayerCamera not assigned!");
  91. if (playerModel == null)
  92. UnityEngine.Debug.LogError("Player model not assigned!");
  93. }
  94.  
  95. void Update()
  96. {
  97. GroundCheck();
  98. Look();
  99. Move();
  100.  
  101. kickTimer -= Time.deltaTime;
  102.  
  103. if (Input.GetKeyDown(KeyCode.Alpha1))
  104. {
  105. isPointing = true;
  106. animator.SetBool("IsPointing", true);
  107. }
  108.  
  109. if (isPointing && Input.GetMouseButtonDown(0))
  110. {
  111. isPointing = false;
  112. animator.SetBool("IsPointing", false);
  113. }
  114.  
  115. if (Input.GetKeyDown(KeyCode.F) && kickTimer <= 0f && !isKicking && isGrounded)
  116. {
  117. StartCoroutine(PerformKick());
  118. }
  119.  
  120. slideCooldownTimer -= Time.deltaTime;
  121.  
  122. Vector3 camPos = playerCamera.localPosition;
  123. camPos.y = Mathf.Lerp(camPos.y, targetCameraY, Time.deltaTime * cameraSmoothSpeed);
  124. playerCamera.localPosition = camPos;
  125.  
  126. UpdateModelPosition();
  127. }
  128.  
  129. void Look()
  130. {
  131. float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
  132. float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
  133.  
  134. cameraPitch -= mouseY;
  135. cameraPitch = Mathf.Clamp(cameraPitch, -maxLookAngle, maxLookAngle);
  136.  
  137. cameraPivot.localRotation = Quaternion.Euler(cameraPitch, 0f, 0f);
  138. transform.Rotate(Vector3.up * mouseX);
  139. }
  140.  
  141. void Move()
  142. {
  143. if (isGrounded && velocity.y < 0)
  144. velocity.y = -2f;
  145.  
  146. float moveX = Input.GetAxis("Horizontal");
  147. float moveZ = Input.GetAxis("Vertical");
  148.  
  149. Vector3 move = transform.right * moveX + transform.forward * moveZ;
  150. float speed = walkSpeed;
  151.  
  152. bool isRunning = Input.GetKey(runKey) && !isSliding && !isCrouching;
  153. if (isRunning) speed = runSpeed;
  154. else if (isCrouching) speed = crouchSpeed;
  155.  
  156. if (Input.GetKeyDown(crouchKey) && Input.GetKey(runKey) && isGrounded && slideCooldownTimer <= 0 && !isSliding)
  157. StartCoroutine(Slide());
  158.  
  159. if (!isSliding && Input.GetKey(crouchKey) && !Input.GetKey(runKey))
  160. StartCrouch();
  161. else if (!isSliding && !Input.GetKey(crouchKey) && isCrouching && CanStandUp())
  162. StopCrouch();
  163.  
  164. controller.Move(move * speed * Time.deltaTime);
  165.  
  166. if (Input.GetButtonDown("Jump") && isGrounded && !isSliding && !isCrouching)
  167. {
  168. velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
  169. animator.SetTrigger("Jump");
  170. }
  171.  
  172. velocity.y += gravity * Time.deltaTime;
  173. controller.Move(velocity * Time.deltaTime);
  174.  
  175. animator.SetFloat("Speed", move.magnitude * speed);
  176. animator.SetBool("IsSliding", isSliding);
  177. animator.SetBool("IsCrouch", isCrouching);
  178. animator.SetBool("IsGrounded", isGrounded);
  179. animator.SetBool("IsFalling", !isGrounded && velocity.y < 0);
  180. }
  181.  
  182. void GroundCheck()
  183. {
  184. Vector3 origin = transform.position + Vector3.up * (controller.radius + 0.1f);
  185. isGrounded = Physics.SphereCast(origin, groundCheckRadius, Vector3.down, out RaycastHit hit, groundCheckDistance, groundMask);
  186. }
  187.  
  188. bool IsHeadBlocked()
  189. {
  190. float radius = controller.radius * 0.95f;
  191. Vector3 bottom = transform.position + Vector3.up * crouchHeight;
  192. Vector3 top = transform.position + Vector3.up * (originalHeight - 0.05f);
  193. return Physics.CheckCapsule(bottom, top, radius, groundMask);
  194. }
  195.  
  196. bool CanStandUp()
  197. {
  198. return !IsHeadBlocked();
  199. }
  200.  
  201. IEnumerator Slide()
  202. {
  203. isSliding = true;
  204. slideCooldownTimer = slideCooldown;
  205.  
  206. controller.height = slideHeight;
  207. controller.center = new Vector3(0, originalCenter.y - (originalHeight - slideHeight) / 2f, 0);
  208. targetCameraY = crouchCameraHeight;
  209.  
  210. float elapsed = 0f;
  211. while (elapsed < slideDuration)
  212. {
  213. Vector3 slideDir = transform.forward;
  214. controller.Move(slideDir * slideSpeed * Time.deltaTime);
  215. elapsed += Time.deltaTime;
  216. yield return null;
  217. }
  218.  
  219. controller.height = crouchHeight;
  220. controller.center = new Vector3(0, originalCenter.y - (originalHeight - crouchHeight) / 2f, 0);
  221. targetCameraY = crouchCameraHeight;
  222.  
  223. isSliding = false;
  224. isCrouching = true;
  225. }
  226.  
  227. IEnumerator PerformKick()
  228. {
  229. isKicking = true;
  230. kickTimer = kickCooldown;
  231.  
  232. animator.SetTrigger("Kick");
  233.  
  234. yield return new WaitForSeconds(0.2f);
  235.  
  236. if (Physics.Raycast(playerCamera.position, playerCamera.forward, out RaycastHit hit, kickRange, kickableLayers))
  237. {
  238. Rigidbody rb = hit.collider.attachedRigidbody;
  239. if (rb != null)
  240. {
  241. Vector3 forceDir = hit.point - playerCamera.position;
  242. forceDir.Normalize();
  243. rb.AddForce(forceDir * kickForce, ForceMode.Impulse);
  244. }
  245. }
  246.  
  247. yield return new WaitForSeconds(0.3f);
  248.  
  249. isKicking = false;
  250. }
  251.  
  252. void StartCrouch()
  253. {
  254. if (isCrouching) return;
  255.  
  256. isCrouching = true;
  257. controller.height = crouchHeight;
  258. controller.center = new Vector3(0, originalCenter.y - (originalHeight - crouchHeight) / 2f, 0);
  259. targetCameraY = crouchCameraHeight;
  260. }
  261.  
  262. void StopCrouch()
  263. {
  264. isCrouching = false;
  265. controller.height = originalHeight;
  266. controller.center = originalCenter;
  267. targetCameraY = originalCameraY;
  268. }
  269.  
  270. void UpdateModelPosition()
  271. {
  272. float modelY = modelYWhenStanding;
  273.  
  274. if (isSliding)
  275. modelY = modelYWhenSliding;
  276. else if (isCrouching)
  277. modelY = modelYWhenCrouching;
  278.  
  279. float targetY = isSliding ? modelY : Mathf.Lerp(playerModel.localPosition.y, modelY, Time.deltaTime * cameraSmoothSpeed);
  280.  
  281. Vector3 modelLocalPos = playerModel.localPosition;
  282. modelLocalPos.y = targetY;
  283. playerModel.localPosition = modelLocalPos;
  284. }
  285.  
  286. void OnDrawGizmosSelected()
  287. {
  288. if (controller == null) return;
  289.  
  290. Gizmos.color = Color.yellow;
  291. float radius = controller.radius * 0.95f;
  292.  
  293. Vector3 bottom = transform.position + Vector3.up * crouchHeight;
  294. Vector3 top = transform.position + Vector3.up * (originalHeight - 0.05f);
  295.  
  296. Gizmos.color = Color.red;
  297. Gizmos.DrawRay(playerCamera.position, playerCamera.forward * kickRange);
  298. Gizmos.DrawWireSphere(bottom, radius);
  299. Gizmos.DrawWireSphere(top, radius);
  300. Gizmos.DrawLine(bottom + Vector3.forward * radius, top + Vector3.forward * radius);
  301. Gizmos.DrawLine(bottom - Vector3.forward * radius, top - Vector3.forward * radius);
  302. Gizmos.DrawLine(bottom + Vector3.right * radius, top + Vector3.right * radius);
  303. Gizmos.DrawLine(bottom - Vector3.right * radius, top - Vector3.right * radius);
  304. }
  305. }
  306.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement