FirstPersonController.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. // CHANGE LOG
  2. //
  3. // CHANGES || version VERSION
  4. //
  5. // "Enable/Disable Headbob, Changed look rotations - should result in reduced camera jitters" || version 1.0.1
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. #if UNITY_EDITOR
  11. using UnityEditor;
  12. using System.Net;
  13. #endif
  14. public class FirstPersonController : MonoBehaviour
  15. {
  16. private Rigidbody rb;
  17. #region Camera Movement Variables
  18. public Camera playerCamera;
  19. public float fov = 60f;
  20. public bool invertCamera = false;
  21. public bool cameraCanMove = true;
  22. public float mouseSensitivity = 2f;
  23. public float maxLookAngle = 50f;
  24. // Crosshair
  25. public bool lockCursor = true;
  26. public bool crosshair = true;
  27. public Sprite crosshairImage;
  28. public Color crosshairColor = Color.white;
  29. // Internal Variables
  30. private float yaw = 0.0f;
  31. private float pitch = 0.0f;
  32. private Image crosshairObject;
  33. #region Camera Zoom Variables
  34. public bool enableZoom = true;
  35. public bool holdToZoom = false;
  36. public KeyCode zoomKey = KeyCode.Mouse1;
  37. public float zoomFOV = 30f;
  38. public float zoomStepTime = 5f;
  39. // Internal Variables
  40. private bool isZoomed = false;
  41. #endregion
  42. #endregion
  43. #region Movement Variables
  44. public bool playerCanMove = true;
  45. public float walkSpeed = 5f;
  46. public float maxVelocityChange = 10f;
  47. // Internal Variables
  48. private bool isWalking = false;
  49. #region Sprint
  50. public bool enableSprint = true;
  51. public bool unlimitedSprint = false;
  52. public KeyCode sprintKey = KeyCode.LeftShift;
  53. public float sprintSpeed = 7f;
  54. public float sprintDuration = 5f;
  55. public float sprintCooldown = .5f;
  56. public float sprintFOV = 80f;
  57. public float sprintFOVStepTime = 10f;
  58. // Sprint Bar
  59. public bool useSprintBar = true;
  60. public bool hideBarWhenFull = true;
  61. public Image sprintBarBG;
  62. public Image sprintBar;
  63. public float sprintBarWidthPercent = .3f;
  64. public float sprintBarHeightPercent = .015f;
  65. // Internal Variables
  66. private CanvasGroup sprintBarCG;
  67. private bool isSprinting = false;
  68. private float sprintRemaining;
  69. private float sprintBarWidth;
  70. private float sprintBarHeight;
  71. private bool isSprintCooldown = false;
  72. private float sprintCooldownReset;
  73. #endregion
  74. #region Jump
  75. public bool enableJump = true;
  76. public KeyCode jumpKey = KeyCode.Space;
  77. public float jumpPower = 5f;
  78. // Internal Variables
  79. private bool isGrounded = false;
  80. #endregion
  81. #region Crouch
  82. public bool enableCrouch = true;
  83. public bool holdToCrouch = true;
  84. public KeyCode crouchKey = KeyCode.LeftControl;
  85. public float crouchHeight = .75f;
  86. public float speedReduction = .5f;
  87. // Internal Variables
  88. private bool isCrouched = false;
  89. private Vector3 originalScale;
  90. #endregion
  91. #endregion
  92. #region Head Bob
  93. public bool enableHeadBob = true;
  94. public Transform joint;
  95. public float bobSpeed = 10f;
  96. public Vector3 bobAmount = new Vector3(.15f, .05f, 0f);
  97. // Internal Variables
  98. private Vector3 jointOriginalPos;
  99. private float timer = 0;
  100. #endregion
  101. private void Awake()
  102. {
  103. rb = GetComponent<Rigidbody>();
  104. crosshairObject = GetComponentInChildren<Image>();
  105. // Set internal variables
  106. playerCamera.fieldOfView = fov;
  107. originalScale = transform.localScale;
  108. jointOriginalPos = joint.localPosition;
  109. if (!unlimitedSprint)
  110. {
  111. sprintRemaining = sprintDuration;
  112. sprintCooldownReset = sprintCooldown;
  113. }
  114. }
  115. void Start()
  116. {
  117. if(lockCursor)
  118. {
  119. Cursor.lockState = CursorLockMode.Locked;
  120. }
  121. if(crosshair)
  122. {
  123. crosshairObject.sprite = crosshairImage;
  124. crosshairObject.color = crosshairColor;
  125. }
  126. else
  127. {
  128. crosshairObject.gameObject.SetActive(false);
  129. }
  130. #region Sprint Bar
  131. sprintBarCG = GetComponentInChildren<CanvasGroup>();
  132. if(useSprintBar)
  133. {
  134. sprintBarBG.gameObject.SetActive(true);
  135. sprintBar.gameObject.SetActive(true);
  136. float screenWidth = Screen.width;
  137. float screenHeight = Screen.height;
  138. sprintBarWidth = screenWidth * sprintBarWidthPercent;
  139. sprintBarHeight = screenHeight * sprintBarHeightPercent;
  140. sprintBarBG.rectTransform.sizeDelta = new Vector3(sprintBarWidth, sprintBarHeight, 0f);
  141. sprintBar.rectTransform.sizeDelta = new Vector3(sprintBarWidth - 2, sprintBarHeight - 2, 0f);
  142. if(hideBarWhenFull)
  143. {
  144. sprintBarCG.alpha = 0;
  145. }
  146. }
  147. else
  148. {
  149. sprintBarBG.gameObject.SetActive(false);
  150. sprintBar.gameObject.SetActive(false);
  151. }
  152. #endregion
  153. }
  154. float camRotation;
  155. private void Update()
  156. {
  157. #region Camera
  158. // Control camera movement
  159. if(cameraCanMove)
  160. {
  161. yaw = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * mouseSensitivity;
  162. if (!invertCamera)
  163. {
  164. pitch -= mouseSensitivity * Input.GetAxis("Mouse Y");
  165. }
  166. else
  167. {
  168. // Inverted Y
  169. pitch += mouseSensitivity * Input.GetAxis("Mouse Y");
  170. }
  171. // Clamp pitch between lookAngle
  172. pitch = Mathf.Clamp(pitch, -maxLookAngle, maxLookAngle);
  173. transform.localEulerAngles = new Vector3(0, yaw, 0);
  174. playerCamera.transform.localEulerAngles = new Vector3(pitch, 0, 0);
  175. }
  176. #region Camera Zoom
  177. if (enableZoom)
  178. {
  179. // Changes isZoomed when key is pressed
  180. // Behavior for toogle zoom
  181. if(Input.GetKeyDown(zoomKey) && !holdToZoom && !isSprinting)
  182. {
  183. if (!isZoomed)
  184. {
  185. isZoomed = true;
  186. }
  187. else
  188. {
  189. isZoomed = false;
  190. }
  191. }
  192. // Changes isZoomed when key is pressed
  193. // Behavior for hold to zoom
  194. if(holdToZoom && !isSprinting)
  195. {
  196. if(Input.GetKeyDown(zoomKey))
  197. {
  198. isZoomed = true;
  199. }
  200. else if(Input.GetKeyUp(zoomKey))
  201. {
  202. isZoomed = false;
  203. }
  204. }
  205. // Lerps camera.fieldOfView to allow for a smooth transistion
  206. if(isZoomed)
  207. {
  208. playerCamera.fieldOfView = Mathf.Lerp(playerCamera.fieldOfView, zoomFOV, zoomStepTime * Time.deltaTime);
  209. }
  210. else if(!isZoomed && !isSprinting)
  211. {
  212. playerCamera.fieldOfView = Mathf.Lerp(playerCamera.fieldOfView, fov, zoomStepTime * Time.deltaTime);
  213. }
  214. }
  215. #endregion
  216. #endregion
  217. #region Sprint
  218. if(enableSprint)
  219. {
  220. if(isSprinting)
  221. {
  222. isZoomed = false;
  223. playerCamera.fieldOfView = Mathf.Lerp(playerCamera.fieldOfView, sprintFOV, sprintFOVStepTime * Time.deltaTime);
  224. // Drain sprint remaining while sprinting
  225. if(!unlimitedSprint)
  226. {
  227. sprintRemaining -= 1 * Time.deltaTime;
  228. if (sprintRemaining <= 0)
  229. {
  230. isSprinting = false;
  231. isSprintCooldown = true;
  232. }
  233. }
  234. }
  235. else
  236. {
  237. // Regain sprint while not sprinting
  238. sprintRemaining = Mathf.Clamp(sprintRemaining += 1 * Time.deltaTime, 0, sprintDuration);
  239. }
  240. // Handles sprint cooldown
  241. // When sprint remaining == 0 stops sprint ability until hitting cooldown
  242. if(isSprintCooldown)
  243. {
  244. sprintCooldown -= 1 * Time.deltaTime;
  245. if (sprintCooldown <= 0)
  246. {
  247. isSprintCooldown = false;
  248. }
  249. }
  250. else
  251. {
  252. sprintCooldown = sprintCooldownReset;
  253. }
  254. // Handles sprintBar
  255. if(useSprintBar && !unlimitedSprint)
  256. {
  257. float sprintRemainingPercent = sprintRemaining / sprintDuration;
  258. sprintBar.transform.localScale = new Vector3(sprintRemainingPercent, 1f, 1f);
  259. }
  260. }
  261. #endregion
  262. #region Jump
  263. // Gets input and calls jump method
  264. if(enableJump && Input.GetKeyDown(jumpKey) && isGrounded)
  265. {
  266. Jump();
  267. }
  268. #endregion
  269. #region Crouch
  270. if (enableCrouch)
  271. {
  272. if(Input.GetKeyDown(crouchKey) && !holdToCrouch)
  273. {
  274. Crouch();
  275. }
  276. if(Input.GetKeyDown(crouchKey) && holdToCrouch)
  277. {
  278. isCrouched = false;
  279. Crouch();
  280. }
  281. else if(Input.GetKeyUp(crouchKey) && holdToCrouch)
  282. {
  283. isCrouched = true;
  284. Crouch();
  285. }
  286. }
  287. #endregion
  288. CheckGround();
  289. if(enableHeadBob)
  290. {
  291. HeadBob();
  292. }
  293. }
  294. void FixedUpdate()
  295. {
  296. #region Movement
  297. if (playerCanMove)
  298. {
  299. // Calculate how fast we should be moving
  300. Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  301. // Checks if player is walking and isGrounded
  302. // Will allow head bob
  303. if (targetVelocity.x != 0 || targetVelocity.z != 0 && isGrounded)
  304. {
  305. isWalking = true;
  306. }
  307. else
  308. {
  309. isWalking = false;
  310. }
  311. // All movement calculations shile sprint is active
  312. if (enableSprint && Input.GetKey(sprintKey) && sprintRemaining > 0f && !isSprintCooldown)
  313. {
  314. targetVelocity = transform.TransformDirection(targetVelocity) * sprintSpeed;
  315. // Apply a force that attempts to reach our target velocity
  316. Vector3 velocity = rb.linearVelocity;
  317. Vector3 velocityChange = (targetVelocity - velocity);
  318. velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
  319. velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
  320. velocityChange.y = 0;
  321. // Player is only moving when valocity change != 0
  322. // Makes sure fov change only happens during movement
  323. if (velocityChange.x != 0 || velocityChange.z != 0)
  324. {
  325. isSprinting = true;
  326. if (isCrouched)
  327. {
  328. Crouch();
  329. }
  330. if (hideBarWhenFull && !unlimitedSprint)
  331. {
  332. sprintBarCG.alpha += 5 * Time.deltaTime;
  333. }
  334. }
  335. rb.AddForce(velocityChange, ForceMode.VelocityChange);
  336. }
  337. // All movement calculations while walking
  338. else
  339. {
  340. isSprinting = false;
  341. if (hideBarWhenFull && sprintRemaining == sprintDuration)
  342. {
  343. sprintBarCG.alpha -= 3 * Time.deltaTime;
  344. }
  345. targetVelocity = transform.TransformDirection(targetVelocity) * walkSpeed;
  346. // Apply a force that attempts to reach our target velocity
  347. Vector3 velocity = rb.linearVelocity;
  348. Vector3 velocityChange = (targetVelocity - velocity);
  349. velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
  350. velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
  351. velocityChange.y = 0;
  352. rb.AddForce(velocityChange, ForceMode.VelocityChange);
  353. }
  354. }
  355. #endregion
  356. }
  357. // Sets isGrounded based on a raycast sent straigth down from the player object
  358. private void CheckGround()
  359. {
  360. Vector3 origin = new Vector3(transform.position.x, transform.position.y - (transform.localScale.y * .5f), transform.position.z);
  361. Vector3 direction = transform.TransformDirection(Vector3.down);
  362. float distance = .75f;
  363. if (Physics.Raycast(origin, direction, out RaycastHit hit, distance))
  364. {
  365. Debug.DrawRay(origin, direction * distance, Color.red);
  366. isGrounded = true;
  367. }
  368. else
  369. {
  370. isGrounded = false;
  371. }
  372. }
  373. private void Jump()
  374. {
  375. // Adds force to the player rigidbody to jump
  376. if (isGrounded)
  377. {
  378. rb.AddForce(0f, jumpPower, 0f, ForceMode.Impulse);
  379. isGrounded = false;
  380. }
  381. // When crouched and using toggle system, will uncrouch for a jump
  382. if(isCrouched && !holdToCrouch)
  383. {
  384. Crouch();
  385. }
  386. }
  387. private void Crouch()
  388. {
  389. // Stands player up to full height
  390. // Brings walkSpeed back up to original speed
  391. if(isCrouched)
  392. {
  393. transform.localScale = new Vector3(originalScale.x, originalScale.y, originalScale.z);
  394. walkSpeed /= speedReduction;
  395. isCrouched = false;
  396. }
  397. // Crouches player down to set height
  398. // Reduces walkSpeed
  399. else
  400. {
  401. transform.localScale = new Vector3(originalScale.x, crouchHeight, originalScale.z);
  402. walkSpeed *= speedReduction;
  403. isCrouched = true;
  404. }
  405. }
  406. private void HeadBob()
  407. {
  408. if(isWalking)
  409. {
  410. // Calculates HeadBob speed during sprint
  411. if(isSprinting)
  412. {
  413. timer += Time.deltaTime * (bobSpeed + sprintSpeed);
  414. }
  415. // Calculates HeadBob speed during crouched movement
  416. else if (isCrouched)
  417. {
  418. timer += Time.deltaTime * (bobSpeed * speedReduction);
  419. }
  420. // Calculates HeadBob speed during walking
  421. else
  422. {
  423. timer += Time.deltaTime * bobSpeed;
  424. }
  425. // Applies HeadBob movement
  426. joint.localPosition = new Vector3(jointOriginalPos.x + Mathf.Sin(timer) * bobAmount.x, jointOriginalPos.y + Mathf.Sin(timer) * bobAmount.y, jointOriginalPos.z + Mathf.Sin(timer) * bobAmount.z);
  427. }
  428. else
  429. {
  430. // Resets when play stops moving
  431. timer = 0;
  432. joint.localPosition = new Vector3(Mathf.Lerp(joint.localPosition.x, jointOriginalPos.x, Time.deltaTime * bobSpeed), Mathf.Lerp(joint.localPosition.y, jointOriginalPos.y, Time.deltaTime * bobSpeed), Mathf.Lerp(joint.localPosition.z, jointOriginalPos.z, Time.deltaTime * bobSpeed));
  433. }
  434. }
  435. }
  436. // Custom Editor
  437. #if UNITY_EDITOR
  438. [CustomEditor(typeof(FirstPersonController)), InitializeOnLoadAttribute]
  439. public class FirstPersonControllerEditor : Editor
  440. {
  441. FirstPersonController fpc;
  442. SerializedObject SerFPC;
  443. private void OnEnable()
  444. {
  445. fpc = (FirstPersonController)target;
  446. SerFPC = new SerializedObject(fpc);
  447. }
  448. public override void OnInspectorGUI()
  449. {
  450. SerFPC.Update();
  451. EditorGUILayout.Space();
  452. GUILayout.Label("Modular First Person Controller", new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleCenter, fontStyle = FontStyle.Bold, fontSize = 16 });
  453. GUILayout.Label("By Jess Case", new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleCenter, fontStyle = FontStyle.Normal, fontSize = 12 });
  454. GUILayout.Label("version 1.0.1", new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleCenter, fontStyle = FontStyle.Normal, fontSize = 12 });
  455. EditorGUILayout.Space();
  456. #region Camera Setup
  457. EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
  458. GUILayout.Label("Camera Setup", new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleCenter, fontStyle = FontStyle.Bold, fontSize = 13 }, GUILayout.ExpandWidth(true));
  459. EditorGUILayout.Space();
  460. fpc.playerCamera = (Camera)EditorGUILayout.ObjectField(new GUIContent("Camera", "Camera attached to the controller."), fpc.playerCamera, typeof(Camera), true);
  461. fpc.fov = EditorGUILayout.Slider(new GUIContent("Field of View", "The camera’s view angle. Changes the player camera directly."), fpc.fov, fpc.zoomFOV, 179f);
  462. fpc.cameraCanMove = EditorGUILayout.ToggleLeft(new GUIContent("Enable Camera Rotation", "Determines if the camera is allowed to move."), fpc.cameraCanMove);
  463. GUI.enabled = fpc.cameraCanMove;
  464. fpc.invertCamera = EditorGUILayout.ToggleLeft(new GUIContent("Invert Camera Rotation", "Inverts the up and down movement of the camera."), fpc.invertCamera);
  465. fpc.mouseSensitivity = EditorGUILayout.Slider(new GUIContent("Look Sensitivity", "Determines how sensitive the mouse movement is."), fpc.mouseSensitivity, .1f, 10f);
  466. fpc.maxLookAngle = EditorGUILayout.Slider(new GUIContent("Max Look Angle", "Determines the max and min angle the player camera is able to look."), fpc.maxLookAngle, 40, 90);
  467. GUI.enabled = true;
  468. fpc.lockCursor = EditorGUILayout.ToggleLeft(new GUIContent("Lock and Hide Cursor", "Turns off the cursor visibility and locks it to the middle of the screen."), fpc.lockCursor);
  469. fpc.crosshair = EditorGUILayout.ToggleLeft(new GUIContent("Auto Crosshair", "Determines if the basic crosshair will be turned on, and sets is to the center of the screen."), fpc.crosshair);
  470. // Only displays crosshair options if crosshair is enabled
  471. if(fpc.crosshair)
  472. {
  473. EditorGUI.indentLevel++;
  474. EditorGUILayout.BeginHorizontal();
  475. EditorGUILayout.PrefixLabel(new GUIContent("Crosshair Image", "Sprite to use as the crosshair."));
  476. fpc.crosshairImage = (Sprite)EditorGUILayout.ObjectField(fpc.crosshairImage, typeof(Sprite), false);
  477. EditorGUILayout.EndHorizontal();
  478. EditorGUILayout.BeginHorizontal();
  479. fpc.crosshairColor = EditorGUILayout.ColorField(new GUIContent("Crosshair Color", "Determines the color of the crosshair."), fpc.crosshairColor);
  480. EditorGUILayout.EndHorizontal();
  481. EditorGUI.indentLevel--;
  482. }
  483. EditorGUILayout.Space();
  484. #region Camera Zoom Setup
  485. GUILayout.Label("Zoom", new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleLeft, fontStyle = FontStyle.Bold, fontSize = 13 }, GUILayout.ExpandWidth(true));
  486. fpc.enableZoom = EditorGUILayout.ToggleLeft(new GUIContent("Enable Zoom", "Determines if the player is able to zoom in while playing."), fpc.enableZoom);
  487. GUI.enabled = fpc.enableZoom;
  488. fpc.holdToZoom = EditorGUILayout.ToggleLeft(new GUIContent("Hold to Zoom", "Requires the player to hold the zoom key instead if pressing to zoom and unzoom."), fpc.holdToZoom);
  489. fpc.zoomKey = (KeyCode)EditorGUILayout.EnumPopup(new GUIContent("Zoom Key", "Determines what key is used to zoom."), fpc.zoomKey);
  490. fpc.zoomFOV = EditorGUILayout.Slider(new GUIContent("Zoom FOV", "Determines the field of view the camera zooms to."), fpc.zoomFOV, .1f, fpc.fov);
  491. fpc.zoomStepTime = EditorGUILayout.Slider(new GUIContent("Step Time", "Determines how fast the FOV transitions while zooming in."), fpc.zoomStepTime, .1f, 10f);
  492. GUI.enabled = true;
  493. #endregion
  494. #endregion
  495. #region Movement Setup
  496. EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
  497. GUILayout.Label("Movement Setup", new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleCenter, fontStyle = FontStyle.Bold, fontSize = 13 }, GUILayout.ExpandWidth(true));
  498. EditorGUILayout.Space();
  499. fpc.playerCanMove = EditorGUILayout.ToggleLeft(new GUIContent("Enable Player Movement", "Determines if the player is allowed to move."), fpc.playerCanMove);
  500. GUI.enabled = fpc.playerCanMove;
  501. fpc.walkSpeed = EditorGUILayout.Slider(new GUIContent("Walk Speed", "Determines how fast the player will move while walking."), fpc.walkSpeed, .1f, fpc.sprintSpeed);
  502. GUI.enabled = true;
  503. EditorGUILayout.Space();
  504. #region Sprint
  505. GUILayout.Label("Sprint", new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleLeft, fontStyle = FontStyle.Bold, fontSize = 13 }, GUILayout.ExpandWidth(true));
  506. fpc.enableSprint = EditorGUILayout.ToggleLeft(new GUIContent("Enable Sprint", "Determines if the player is allowed to sprint."), fpc.enableSprint);
  507. GUI.enabled = fpc.enableSprint;
  508. fpc.unlimitedSprint = EditorGUILayout.ToggleLeft(new GUIContent("Unlimited Sprint", "Determines if 'Sprint Duration' is enabled. Turning this on will allow for unlimited sprint."), fpc.unlimitedSprint);
  509. fpc.sprintKey = (KeyCode)EditorGUILayout.EnumPopup(new GUIContent("Sprint Key", "Determines what key is used to sprint."), fpc.sprintKey);
  510. fpc.sprintSpeed = EditorGUILayout.Slider(new GUIContent("Sprint Speed", "Determines how fast the player will move while sprinting."), fpc.sprintSpeed, fpc.walkSpeed, 20f);
  511. //GUI.enabled = !fpc.unlimitedSprint;
  512. fpc.sprintDuration = EditorGUILayout.Slider(new GUIContent("Sprint Duration", "Determines how long the player can sprint while unlimited sprint is disabled."), fpc.sprintDuration, 1f, 20f);
  513. fpc.sprintCooldown = EditorGUILayout.Slider(new GUIContent("Sprint Cooldown", "Determines how long the recovery time is when the player runs out of sprint."), fpc.sprintCooldown, .1f, fpc.sprintDuration);
  514. //GUI.enabled = true;
  515. fpc.sprintFOV = EditorGUILayout.Slider(new GUIContent("Sprint FOV", "Determines the field of view the camera changes to while sprinting."), fpc.sprintFOV, fpc.fov, 179f);
  516. fpc.sprintFOVStepTime = EditorGUILayout.Slider(new GUIContent("Step Time", "Determines how fast the FOV transitions while sprinting."), fpc.sprintFOVStepTime, .1f, 20f);
  517. fpc.useSprintBar = EditorGUILayout.ToggleLeft(new GUIContent("Use Sprint Bar", "Determines if the default sprint bar will appear on screen."), fpc.useSprintBar);
  518. // Only displays sprint bar options if sprint bar is enabled
  519. if(fpc.useSprintBar)
  520. {
  521. EditorGUI.indentLevel++;
  522. EditorGUILayout.BeginHorizontal();
  523. fpc.hideBarWhenFull = EditorGUILayout.ToggleLeft(new GUIContent("Hide Full Bar", "Hides the sprint bar when sprint duration is full, and fades the bar in when sprinting. Disabling this will leave the bar on screen at all times when the sprint bar is enabled."), fpc.hideBarWhenFull);
  524. EditorGUILayout.EndHorizontal();
  525. EditorGUILayout.BeginHorizontal();
  526. EditorGUILayout.PrefixLabel(new GUIContent("Bar BG", "Object to be used as sprint bar background."));
  527. fpc.sprintBarBG = (Image)EditorGUILayout.ObjectField(fpc.sprintBarBG, typeof(Image), true);
  528. EditorGUILayout.EndHorizontal();
  529. EditorGUILayout.BeginHorizontal();
  530. EditorGUILayout.PrefixLabel(new GUIContent("Bar", "Object to be used as sprint bar foreground."));
  531. fpc.sprintBar = (Image)EditorGUILayout.ObjectField(fpc.sprintBar, typeof(Image), true);
  532. EditorGUILayout.EndHorizontal();
  533. EditorGUILayout.BeginHorizontal();
  534. fpc.sprintBarWidthPercent = EditorGUILayout.Slider(new GUIContent("Bar Width", "Determines the width of the sprint bar."), fpc.sprintBarWidthPercent, .1f, .5f);
  535. EditorGUILayout.EndHorizontal();
  536. EditorGUILayout.BeginHorizontal();
  537. fpc.sprintBarHeightPercent = EditorGUILayout.Slider(new GUIContent("Bar Height", "Determines the height of the sprint bar."), fpc.sprintBarHeightPercent, .001f, .025f);
  538. EditorGUILayout.EndHorizontal();
  539. EditorGUI.indentLevel--;
  540. }
  541. GUI.enabled = true;
  542. EditorGUILayout.Space();
  543. #endregion
  544. #region Jump
  545. GUILayout.Label("Jump", new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleLeft, fontStyle = FontStyle.Bold, fontSize = 13 }, GUILayout.ExpandWidth(true));
  546. fpc.enableJump = EditorGUILayout.ToggleLeft(new GUIContent("Enable Jump", "Determines if the player is allowed to jump."), fpc.enableJump);
  547. GUI.enabled = fpc.enableJump;
  548. fpc.jumpKey = (KeyCode)EditorGUILayout.EnumPopup(new GUIContent("Jump Key", "Determines what key is used to jump."), fpc.jumpKey);
  549. fpc.jumpPower = EditorGUILayout.Slider(new GUIContent("Jump Power", "Determines how high the player will jump."), fpc.jumpPower, .1f, 20f);
  550. GUI.enabled = true;
  551. EditorGUILayout.Space();
  552. #endregion
  553. #region Crouch
  554. GUILayout.Label("Crouch", new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleLeft, fontStyle = FontStyle.Bold, fontSize = 13 }, GUILayout.ExpandWidth(true));
  555. fpc.enableCrouch = EditorGUILayout.ToggleLeft(new GUIContent("Enable Crouch", "Determines if the player is allowed to crouch."), fpc.enableCrouch);
  556. GUI.enabled = fpc.enableCrouch;
  557. fpc.holdToCrouch = EditorGUILayout.ToggleLeft(new GUIContent("Hold To Crouch", "Requires the player to hold the crouch key instead if pressing to crouch and uncrouch."), fpc.holdToCrouch);
  558. fpc.crouchKey = (KeyCode)EditorGUILayout.EnumPopup(new GUIContent("Crouch Key", "Determines what key is used to crouch."), fpc.crouchKey);
  559. fpc.crouchHeight = EditorGUILayout.Slider(new GUIContent("Crouch Height", "Determines the y scale of the player object when crouched."), fpc.crouchHeight, .1f, 1);
  560. fpc.speedReduction = EditorGUILayout.Slider(new GUIContent("Speed Reduction", "Determines the percent 'Walk Speed' is reduced by. 1 being no reduction, and .5 being half."), fpc.speedReduction, .1f, 1);
  561. GUI.enabled = true;
  562. #endregion
  563. #endregion
  564. #region Head Bob
  565. EditorGUILayout.Space();
  566. EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
  567. GUILayout.Label("Head Bob Setup", new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleCenter, fontStyle = FontStyle.Bold, fontSize = 13 }, GUILayout.ExpandWidth(true));
  568. EditorGUILayout.Space();
  569. fpc.enableHeadBob = EditorGUILayout.ToggleLeft(new GUIContent("Enable Head Bob", "Determines if the camera will bob while the player is walking."), fpc.enableHeadBob);
  570. GUI.enabled = fpc.enableHeadBob;
  571. fpc.joint = (Transform)EditorGUILayout.ObjectField(new GUIContent("Camera Joint", "Joint object position is moved while head bob is active."), fpc.joint, typeof(Transform), true);
  572. fpc.bobSpeed = EditorGUILayout.Slider(new GUIContent("Speed", "Determines how often a bob rotation is completed."), fpc.bobSpeed, 1, 20);
  573. fpc.bobAmount = EditorGUILayout.Vector3Field(new GUIContent("Bob Amount", "Determines the amount the joint moves in both directions on every axes."), fpc.bobAmount);
  574. GUI.enabled = true;
  575. #endregion
  576. //Sets any changes from the prefab
  577. if(GUI.changed)
  578. {
  579. EditorUtility.SetDirty(fpc);
  580. Undo.RecordObject(fpc, "FPC Change");
  581. SerFPC.ApplyModifiedProperties();
  582. }
  583. }
  584. }
  585. #endif