Advertisement
Viksy

PlayerController

Sep 25th, 2022
1,189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 21.60 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using TMPro;
  5.  
  6. public class PlayerController : MonoBehaviour
  7. {
  8.     public bool CanMove { get; private set; } = true;
  9.     private bool isSprinting => canSprint && Input.GetKey(sprintKey) && !isCrouching;
  10.     private bool shouldJump => Input.GetKeyDown(jumpKey) && characterController.isGrounded && !isCrouching;
  11.     private bool shouldCrouch => Input.GetKeyDown(crouchKey) && characterController.isGrounded;
  12.  
  13.     [Header("Functional Options")]
  14.     [SerializeField] private bool canSprint = true;
  15.     [SerializeField] private bool canJump = true;
  16.     [SerializeField] private bool canCrouch = true;
  17.     [SerializeField] private bool canUseStamina = true;
  18.     [SerializeField] private bool canUseHeadbob = true;
  19.     [SerializeField] private bool canUseSlopes = true;
  20.     [SerializeField] private bool canZoom = true;
  21.     [SerializeField] private bool canUseFootsteps = true;
  22.     [SerializeField] private bool canInteract = true;
  23.     [SerializeField] private bool canUseAnimation = true;
  24.     [SerializeField] private bool canUseFlashlight = true;
  25.  
  26.     [Header("Controls")]
  27.     [SerializeField] private KeyCode sprintKey = KeyCode.LeftShift;
  28.     [SerializeField] private KeyCode jumpKey = KeyCode.Space;
  29.     [SerializeField] private KeyCode crouchKey = KeyCode.LeftControl;
  30.     [SerializeField] private KeyCode zoomKey = KeyCode.Mouse2;
  31.     [SerializeField] private KeyCode interactKey = KeyCode.Mouse0;
  32.     [SerializeField] private KeyCode flashlightKey = KeyCode.E;
  33.  
  34.     [Header("Movement Parameters")]
  35.     [SerializeField] private float walkSpeed = 3.0f;
  36.     [SerializeField] private float sprintSpeed = 6.0f;
  37.     [SerializeField] private float crouchSpeed = 1.5f;
  38.     [SerializeField] private float slopeSpeed = 8f;
  39.  
  40.     [Header("Look Parameters")]
  41.     [SerializeField, Range(1, 10)] private float lookSpeedX = 2.0f;
  42.     [SerializeField, Range(1, 10)] private float lookSpeedY = 2.0f;
  43.     [SerializeField, Range(1, 180)] private float upperLookLimit = 80f;
  44.     [SerializeField, Range(1, 180)] private float lowerLookLimit = 80f;
  45.  
  46.     [Header("Jumping Parameters")]
  47.     [SerializeField] private float jumpForce = 8.0f;
  48.     [SerializeField] private float gravity = 30.0f;
  49.  
  50.     [Header("Crouch Parameters")]
  51.     [SerializeField] private float crouchHeight = 0.5f;
  52.     [SerializeField] private float standingHeight = 2f;
  53.     [SerializeField] private float timeToCrouch = 0.25f;
  54.     [SerializeField] private Vector3 crouchingCenter = new Vector3(0, 0.5f, 0);
  55.     [SerializeField] private Vector3 standingCenter = new Vector3(0, 0, 0);
  56.     private bool isCrouching = false;
  57.     private bool duringCrouchAnimation = false;
  58.  
  59.     [Header("Stamina Parameters")]
  60.     [SerializeField] private float maxStamina = 100f;
  61.     [SerializeField] private float staminaUseMultiplier = 5f;
  62.     [SerializeField] private float timeBeforeStaminaRegenStarts = 5f;
  63.     [SerializeField] private float staminaValueIncrement = 2f;
  64.     [SerializeField] private float staminaTimeIncrement = 0.1f;
  65.     public float currentStamina;
  66.     private Coroutine regeneratingStamina;
  67.     public static Action<float> OnStaminaChange;
  68.  
  69.     [Header("Headbob Parameters")]
  70.     [SerializeField] private float walkBobSpeed = 14f;
  71.     [SerializeField] private float walkBobAmount = 0.05f;
  72.     [SerializeField] private float sprintBobSpeed = 18f;
  73.     [SerializeField] private float sprintBobAmount = 0.11f;
  74.     [SerializeField] private float crouchBobSpeed = 8f;
  75.     [SerializeField] private float crouchBobAmount = 0.025f;
  76.     private float defaultYPos = 0;
  77.     private float timer;
  78.  
  79.     [Header("Slope Parameters")]
  80.     [SerializeField] private float slopeForce;
  81.     [SerializeField] private float slopeForceRayLength;
  82.     private RaycastHit slopeSlideHit;
  83.     private RaycastHit slopeWalkHit;
  84.     private bool isSlopeSliding
  85.     {
  86.         get
  87.         {
  88.             if (characterController.isGrounded && Physics.Raycast(transform.position, Vector3.down, out slopeSlideHit, slopeForceRayLength))
  89.             {
  90.                 return Vector3.Angle(slopeSlideHit.normal, Vector3.up) > characterController.slopeLimit;
  91.             }
  92.             else
  93.             {
  94.                 return false;
  95.             }
  96.         }
  97.     }
  98.     private bool isSlopeWalking()
  99.     {
  100.         if (Physics.Raycast(transform.position, Vector3.down, out slopeWalkHit, characterController.height / 2 + slopeForceRayLength))
  101.         {
  102.             return (slopeWalkHit.normal != Vector3.up) && (Vector3.Angle(slopeWalkHit.normal, Vector3.up) < characterController.slopeLimit);
  103.         }
  104.         else
  105.         {
  106.             return false;
  107.         }
  108.     }
  109.  
  110.     [Header("Zoom Parameters")]
  111.     [SerializeField] private float timeToZoom = 0.3f;
  112.     [SerializeField] private float zoomFOV = 45f;
  113.     private float defaultFOV;
  114.     private Coroutine zoomRoutine;
  115.  
  116.     [Header("Footsteps Parameters")]
  117.     [SerializeField] private float baseStepSpeed = 0.5f;
  118.     [SerializeField] private float crouchStepMultiplier = 1.5f;
  119.     [SerializeField] private float sprintStepMultiplier = 0.6f;
  120.     [SerializeField] private AudioSource footstepAudioSource = default;
  121.     [SerializeField] private AudioClip[] stoneClips = default;
  122.     [SerializeField] private AudioClip[] metalClips = default;
  123.     private float footstepTimer = 0f;
  124.     private float GetCurrentOffset => isCrouching ? baseStepSpeed * crouchStepMultiplier : isSprinting ? baseStepSpeed * sprintStepMultiplier : baseStepSpeed;
  125.  
  126.     [Header("Interaction")]
  127.     [SerializeField] private Vector3 interactionRayPoint = default;
  128.     [SerializeField] private float interactionDistance = default;
  129.     [SerializeField] private LayerMask interactionLayer = default;
  130.     public GameObject interactOB;
  131.     public bool shouldRemoveText = true;
  132.     public TextMeshProUGUI textDisplay;
  133.     private Interactable currentInteractable;
  134.  
  135.     [Header("Flashlight")]
  136.     [SerializeField] private GameObject flashlight;
  137.     [SerializeField] public bool isEquip;
  138.     [SerializeField] private float delayTime;
  139.     [SerializeField] private AudioSource flashlightAudioSource = default;
  140.     [SerializeField] private AudioClip flashlightOnOff;
  141.  
  142.     [Header("SFX")]
  143.     [SerializeField] private AudioSource heartBeat;
  144.     [SerializeField] private AudioSource pickUp;
  145.  
  146.     private Camera playerCamera;
  147.     private CharacterController characterController;
  148.  
  149.     private Vector3 moveDirection;
  150.     private Vector2 currentInput;
  151.     private bool isMoving = false;
  152.  
  153.     private float rotationX = 0f;
  154.  
  155.     public static PlayerController instance;
  156.     public bool canUseMovement = true;
  157.     public bool canUseMouse = true;
  158.  
  159.     void Awake()
  160.     {
  161.         instance = this;        
  162.        
  163.         interactOB.SetActive(false);        
  164.  
  165.         isEquip = false;
  166.         flashlight.SetActive(false);
  167.  
  168.         playerCamera = GetComponentInChildren<Camera>();
  169.         characterController = GetComponent<CharacterController>();
  170.  
  171.         defaultYPos = playerCamera.transform.localPosition.y;
  172.         defaultFOV = playerCamera.fieldOfView;
  173.  
  174.         currentStamina = maxStamina;
  175.  
  176.         StartCoroutine(CrouchStand());
  177.  
  178.         Cursor.lockState = CursorLockMode.Locked;
  179.         Cursor.visible = false;
  180.     }
  181.  
  182.     void Update()
  183.     {
  184.         if (CanMove)
  185.         {
  186.             if (canUseMovement)
  187.             {
  188.                 HandleMovementInput();
  189.             }
  190.             else
  191.             {
  192.                 currentInput = Vector2.zero;
  193.                 moveDirection = Vector3.zero;
  194.             }
  195.  
  196.             if (canUseMouse)
  197.             {
  198.                 HandleMouseLook();
  199.             }
  200.  
  201.             isMoving = Mathf.Abs(moveDirection.x) < 0.01f || Mathf.Abs(moveDirection.z) < 0.02f ? false : true;
  202.  
  203.             if (canJump)
  204.             {
  205.                 HandleJump();
  206.             }
  207.             if (canCrouch)
  208.             {
  209.                 HandleCrouch();
  210.             }
  211.             if (canUseStamina)
  212.             {
  213.                 HandleStamina();
  214.             }
  215.             if (canUseHeadbob)
  216.             {
  217.                 HandleHeadbob();
  218.             }
  219.             if (canUseSlopes)
  220.             {
  221.                 HandleSlopes();
  222.             }
  223.             if (canZoom)
  224.             {
  225.                 HandleZoom();
  226.             }
  227.             if (canUseFootsteps)
  228.             {
  229.                 HandleFootsteps();
  230.             }
  231.             if (canInteract)
  232.             {
  233.                 HandleInteractionCheck();
  234.                 HandleInteractionInput();
  235.             }
  236.             if (canUseFlashlight)
  237.             {
  238.                 HandleFlashlight();
  239.             }
  240.             ApplyFinalMovements();
  241.         }
  242.     }
  243.  
  244.     private void HandleMovementInput()
  245.     {
  246.         currentInput = new Vector2((isCrouching ? crouchSpeed : isSprinting ? sprintSpeed : walkSpeed) * Input.GetAxis("Vertical"), (isCrouching ? crouchSpeed : isSprinting ? sprintSpeed : walkSpeed) * Input.GetAxis("Horizontal"));
  247.  
  248.         float moveDirectionY = moveDirection.y;
  249.         moveDirection = (transform.TransformDirection(Vector3.forward) * currentInput.x) + (transform.TransformDirection(Vector3.right) * currentInput.y);
  250.         moveDirection.y = moveDirectionY;
  251.     }
  252.  
  253.     private void HandleMouseLook()
  254.     {
  255.         rotationX -= Input.GetAxis("Mouse Y") * GetSettings.instance.mouseSens;
  256.         rotationX = Mathf.Clamp(rotationX, -upperLookLimit, lowerLookLimit);
  257.         playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
  258.         transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * GetSettings.instance.mouseSens, 0);
  259.     }
  260.  
  261.     private void HandleJump()
  262.     {
  263.         if (shouldJump)
  264.         {
  265.             moveDirection.y = jumpForce;
  266.         }
  267.     }
  268.  
  269.     private void HandleCrouch()
  270.     {
  271.         if (shouldCrouch)
  272.         {
  273.             StartCoroutine(CrouchStand());
  274.         }
  275.     }
  276.  
  277.     private IEnumerator CrouchStand()
  278.     {
  279.         if (isCrouching && Physics.Raycast(playerCamera.transform.position, Vector3.up, 2f))
  280.         {
  281.             yield break;
  282.         }
  283.  
  284.         duringCrouchAnimation = !duringCrouchAnimation;
  285.  
  286.         float timeElapsed = 0f;
  287.         float targetHeight = isCrouching ? standingHeight : crouchHeight;
  288.         float currentHeight = characterController.height;
  289.         Vector3 targetCenter = isCrouching ? standingCenter : crouchingCenter;
  290.         Vector3 currentCenter = characterController.center;
  291.  
  292.         while (timeElapsed < timeToCrouch)
  293.         {
  294.             characterController.height = Mathf.Lerp(currentHeight, targetHeight, timeElapsed / timeToCrouch);
  295.             characterController.center = Vector3.Lerp(currentCenter, targetCenter, timeElapsed / timeToCrouch);
  296.             timeElapsed += Time.deltaTime;
  297.             yield return null;
  298.         }
  299.  
  300.         characterController.height = targetHeight;
  301.         characterController.center = targetCenter;
  302.  
  303.         isCrouching = !isCrouching;
  304.     }
  305.  
  306.     private void HandleStamina()
  307.     {
  308.         if (isSprinting && currentInput != Vector2.zero)
  309.         {
  310.             if (regeneratingStamina != null)
  311.             {
  312.                 StopCoroutine(regeneratingStamina);
  313.                 regeneratingStamina = null;
  314.             }
  315.  
  316.             currentStamina -= staminaUseMultiplier * Time.deltaTime;
  317.  
  318.             if (currentStamina < 0f)
  319.             {
  320.                 currentStamina = 0f;
  321.             }
  322.  
  323.             OnStaminaChange?.Invoke(currentStamina);
  324.  
  325.             if (currentStamina <= 0)
  326.             {
  327.                 canSprint = false;
  328.             }
  329.         }
  330.  
  331.         if (!isSprinting && currentStamina < maxStamina && regeneratingStamina == null)
  332.         {
  333.             regeneratingStamina = StartCoroutine(RegenerateStamina());
  334.         }
  335.     }
  336.  
  337.     private IEnumerator RegenerateStamina()
  338.     {
  339.         yield return new WaitForSeconds(timeBeforeStaminaRegenStarts);
  340.         WaitForSeconds timeToWait = new WaitForSeconds(staminaTimeIncrement);
  341.  
  342.         while (currentStamina < maxStamina)
  343.         {
  344.             if (currentStamina > 0f)
  345.             {
  346.                 canSprint = true;
  347.             }
  348.  
  349.             currentStamina += staminaValueIncrement;
  350.  
  351.             if (currentStamina > maxStamina)
  352.             {
  353.                 currentStamina = maxStamina;
  354.             }
  355.  
  356.             OnStaminaChange?.Invoke(currentStamina);
  357.  
  358.             yield return timeToWait;
  359.         }
  360.  
  361.         regeneratingStamina = null;
  362.     }
  363.  
  364.     private void HandleHeadbob()
  365.     {
  366.         if (!characterController.isGrounded) return;
  367.  
  368.         if (Mathf.Abs(moveDirection.x) > 0.1f || Mathf.Abs(moveDirection.z) > 0.1f)
  369.         {
  370.             timer += Time.deltaTime * (isCrouching ? crouchBobSpeed : isSprinting ? sprintBobSpeed : walkBobSpeed);
  371.             playerCamera.transform.localPosition = new Vector3(
  372.                 playerCamera.transform.localPosition.x,
  373.                 defaultYPos + Mathf.Sin(timer) * (isCrouching ? crouchBobAmount : isSprinting ? sprintBobAmount : walkBobAmount),
  374.                 playerCamera.transform.localPosition.z);
  375.         }
  376.     }
  377.  
  378.     private void HandleSlopes()
  379.     {
  380.         if (isSlopeSliding)
  381.         {
  382.             moveDirection += new Vector3(slopeSlideHit.normal.x, -slopeSlideHit.normal.y, slopeSlideHit.normal.z) * slopeSpeed;
  383.         }
  384.         if (isSlopeWalking())
  385.         {
  386.             characterController.Move(Vector3.down * characterController.height / 2 * slopeForce * Time.deltaTime);
  387.         }
  388.     }
  389.  
  390.     private void HandleZoom()
  391.     {
  392.         if (Input.GetKeyDown(zoomKey))
  393.         {
  394.             if (zoomRoutine != null)
  395.             {
  396.                 StopCoroutine(zoomRoutine);
  397.                 zoomRoutine = null;
  398.             }
  399.  
  400.             zoomRoutine = StartCoroutine(ToggleZoom(true));
  401.         }
  402.  
  403.         if (Input.GetKeyUp(zoomKey))
  404.         {
  405.             if (zoomRoutine != null)
  406.             {
  407.                 StopCoroutine(zoomRoutine);
  408.                 zoomRoutine = null;
  409.             }
  410.  
  411.             zoomRoutine = StartCoroutine(ToggleZoom(false));
  412.         }
  413.     }
  414.  
  415.     private IEnumerator ToggleZoom(bool isEnter)
  416.     {
  417.         float targetFOV = isEnter ? zoomFOV : defaultFOV;
  418.         float startingFOV = playerCamera.fieldOfView;
  419.         float timeElapsed = 0f;
  420.  
  421.         while (timeElapsed < timeToZoom)
  422.         {
  423.             playerCamera.fieldOfView = Mathf.Lerp(startingFOV, targetFOV, timeElapsed / timeToZoom);
  424.             timeElapsed += Time.deltaTime;
  425.             yield return null;
  426.         }
  427.  
  428.         playerCamera.fieldOfView = targetFOV;
  429.         zoomRoutine = null;
  430.     }
  431.  
  432.     private void HandleFootsteps()
  433.     {
  434.         if (currentInput == Vector2.zero) return;
  435.  
  436.         footstepTimer -= Time.deltaTime;
  437.  
  438.         if (footstepTimer <= 0)
  439.         {
  440.             if (Physics.Raycast(playerCamera.transform.position, Vector3.down, out RaycastHit hit, 4f))
  441.             {
  442.                 switch (hit.collider.tag)
  443.                 {
  444.                     case "Footsteps/Stone":
  445.                         footstepAudioSource.PlayOneShot(stoneClips[UnityEngine.Random.Range(0, stoneClips.Length - 1)]);
  446.                         break;
  447.                     case "Footsteps/Metal":
  448.                         footstepAudioSource.PlayOneShot(metalClips[UnityEngine.Random.Range(0, stoneClips.Length - 1)]);
  449.                         break;
  450.                     default:
  451.                         footstepAudioSource.PlayOneShot(stoneClips[UnityEngine.Random.Range(0, stoneClips.Length - 1)]);
  452.                         break;
  453.                 }
  454.             }
  455.  
  456.             footstepTimer = GetCurrentOffset;
  457.         }
  458.     }
  459.  
  460.     private void HandleInteractionCheck()
  461.     {
  462.         if (Physics.Raycast(playerCamera.ViewportPointToRay(interactionRayPoint), out RaycastHit hit, interactionDistance))
  463.         {
  464.             if (hit.collider.gameObject.layer == 6 && (currentInteractable == null || hit.collider.gameObject.GetInstanceID() != currentInteractable.GetInstanceID()))
  465.             {
  466.                 hit.collider.TryGetComponent(out currentInteractable);
  467.  
  468.                 if (currentInteractable)
  469.                 {
  470.                     currentInteractable.OnFocus();
  471.                 }
  472.             }
  473.         }
  474.         else if (currentInteractable)
  475.         {
  476.             currentInteractable.OnLoseFocus();
  477.             currentInteractable = null;
  478.         }
  479.  
  480.         if (currentInteractable == null)
  481.         {
  482.             if(shouldRemoveText)
  483.             {
  484.                 textDisplay.text = "";
  485.             }
  486.  
  487.             if(shouldRemoveText == false)
  488.             {
  489.                 canInteract = false;
  490.             }
  491.            
  492.             interactOB.SetActive(false);
  493.         }
  494.     }
  495.  
  496.     private void HandleInteractionInput()
  497.     {
  498.         if (Input.GetKeyDown(interactKey) && currentInteractable != null && Physics.Raycast(playerCamera.ViewportPointToRay(interactionRayPoint), out RaycastHit hit, interactionDistance, interactionLayer))
  499.         {
  500.             currentInteractable.OnInteract();
  501.         }
  502.     }
  503.  
  504.     //private void HandleAnimation()
  505.     //{
  506.     //    if (isEquip)
  507.     //    {
  508.     //        animator.SetBool("isEquip", true);
  509.     //        lantern.SetBool("isEquip", true);
  510.     //        if (lanternLightOff)
  511.     //        {
  512.     //            lanternMat.DisableKeyword("_EMISSION");
  513.     //            lantern.SetBool("isLightOff", true);
  514.     //            isEquip = true;
  515.     //        }
  516.     //        else
  517.     //        {
  518.     //            lanternMat.EnableKeyword("_EMISSION");
  519.     //            lantern.SetBool("isLightOff", false);
  520.     //        }
  521.     //    }
  522.     //    else
  523.     //    {
  524.     //        animator.SetBool("isEquip", false);
  525.     //        lantern.SetBool("isEquip", false);
  526.     //    }
  527.  
  528.     //    if (!isMoving && isEquip)
  529.     //    {
  530.     //        animator.SetFloat("speedEquip", 0f, dampTime, Time.deltaTime);
  531.     //    }
  532.     //    else if (isMoving && isEquip && !isSprinting)
  533.     //    {
  534.     //        animator.SetFloat("speedEquip", 0.5f, dampTime, Time.deltaTime);
  535.     //    }
  536.     //    else if (isMoving && isEquip && isSprinting)
  537.     //    {
  538.     //        animator.SetFloat("speedEquip", 1f, dampTime, Time.deltaTime);
  539.     //    }
  540.  
  541.     //    if (!isMoving && !isEquip)
  542.     //    {
  543.     //        animator.SetFloat("speedDown", 0f, dampTime, Time.deltaTime);
  544.     //    }
  545.     //    else if (isMoving && !isEquip)
  546.     //    {
  547.     //        animator.SetFloat("speedDown", 0.5f, dampTime, Time.deltaTime);
  548.     //    }
  549.  
  550.     //    if (duringCrouchAnimation)
  551.     //    {
  552.     //        animator.SetBool("isCrouch", true);
  553.     //    }
  554.     //    else
  555.     //    {
  556.     //        animator.SetBool("isCrouch", false);
  557.     //    }
  558.     //}
  559.  
  560.     private void HandleFlashlight()
  561.     {
  562.         if (Input.GetKeyDown(flashlightKey))
  563.         {
  564.             StartCoroutine(UseFlashlight());
  565.         }
  566.     }
  567.  
  568.     public IEnumerator UseFlashlight()
  569.     {
  570.         isEquip = !isEquip;
  571.         flashlightAudioSource.PlayOneShot(flashlightOnOff);
  572.         canUseFlashlight = false;
  573.  
  574.         if (isEquip)
  575.         {
  576.             flashlight.SetActive(true);
  577.         }
  578.  
  579.         if(!isEquip)
  580.         {
  581.             flashlight.SetActive(false);
  582.         }
  583.  
  584.         yield return new WaitForSeconds(delayTime);
  585.  
  586.         canUseFlashlight = true;
  587.     }
  588.  
  589.     //public IEnumerator flashlightSwitch(float timeBetweenSwitch)
  590.     //{
  591.     //    lanternLightOff = true;
  592.  
  593.     //    yield return new WaitForSeconds(timeBetweenSwitch);
  594.  
  595.     //    lanternLightOff = false;
  596.     //}
  597.  
  598.     private void ApplyFinalMovements()
  599.     {
  600.         if (!characterController.isGrounded)
  601.         {
  602.             moveDirection.y -= gravity * Time.deltaTime;          
  603.         }
  604.  
  605.         characterController.Move(moveDirection * Time.deltaTime);
  606.  
  607.         if (characterController.velocity.y < -1 && characterController.isGrounded)
  608.         {
  609.             moveDirection.y = 0;
  610.         }
  611.     }
  612.  
  613.     public void heartBeatUp(float timeToChange, float highValue, float timeToGoBack)
  614.     {
  615.         StartCoroutine(higherHeartBeat(timeToChange, highValue, timeToGoBack));
  616.     }
  617.  
  618.     public void pickUpSFX()
  619.     {
  620.         pickUp.Play();
  621.     }
  622.  
  623.     IEnumerator higherHeartBeat(float timeToChange, float highValue, float timeToGoBack)
  624.     {
  625.         float timeElapsed = 0f;
  626.         float startingVolume = heartBeat.volume;
  627.  
  628.         while (timeElapsed < timeToChange)
  629.         {
  630.             heartBeat.volume = Mathf.Lerp(startingVolume, highValue, timeElapsed / timeToChange);
  631.             timeElapsed += Time.deltaTime;
  632.             yield return null;
  633.         }
  634.  
  635.         heartBeat.volume = highValue;
  636.  
  637.         yield return new WaitForSeconds(timeToGoBack);
  638.  
  639.         timeElapsed = 0f;
  640.         startingVolume = heartBeat.volume;
  641.  
  642.         while (timeElapsed < timeToChange)
  643.         {
  644.             heartBeat.volume = Mathf.Lerp(startingVolume, 0f, timeElapsed / timeToChange);
  645.             timeElapsed += Time.deltaTime;
  646.             yield return null;
  647.         }
  648.  
  649.         heartBeat.volume = 0f;
  650.     }
  651.  
  652.     public void EnableFlashlightUse()
  653.     {
  654.         canUseFlashlight = true;
  655.     }
  656.  
  657.     public void DisableFlashlightUse()
  658.     {
  659.         canUseFlashlight = false;
  660.     }
  661.  
  662.     public void EnableCrouchUse()
  663.     {
  664.         canCrouch = true;
  665.     }
  666.  
  667.     public void EnableZoomUse()
  668.     {
  669.         canZoom = true;
  670.     }
  671.  
  672.     public void EnableInteractUse()
  673.     {
  674.         canInteract = true;
  675.     }
  676.  
  677.     public void EnableSprintUse()
  678.     {
  679.         canSprint = true;
  680.     }
  681.  
  682.     public void LockPlayer()
  683.     {
  684.         canUseMouse = false;
  685.         canUseMovement = false;
  686.     }
  687.  
  688.     public void UnlockPlayer()
  689.     {
  690.         canUseMouse = true;
  691.         canUseMovement = true;
  692.     }
  693. }
  694.  
  695.  
  696.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement