FirstPersonController.cs 28 KB

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