Advertisement
Guest User

Movement

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