eastbayeff

Untitled

May 11th, 2022
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7.     private CharacterController controller;
  8.     private Vector3 playerVelocity;
  9.     private bool IsGrounded;
  10.     public float speed = 5f;
  11.     public float gravity = -9.8f;
  12.     public float jumpHeight = 3f;
  13.     private bool lerpCrouch;
  14.     private bool crouching;
  15.     private bool isSprinting;
  16.  
  17.     private bool lockCursor = true;
  18.  
  19.     public float crouchTimer = 1f;
  20.  
  21.     public Camera myCam;
  22.     public float walkFOV = 60f;
  23.     public float runFOV = 120f;
  24.  
  25.     // Start is called before the first frame update
  26.     void Start()
  27.     {
  28.         if (lockCursor)
  29.         {
  30.             Cursor.lockState = CursorLockMode.Locked;
  31.             Cursor.visible = false;
  32.         }
  33.         controller = GetComponent<CharacterController>();
  34.  
  35.         myCam.fieldOfView = walkFOV;
  36.     }
  37.  
  38.     // Update is called once per frame
  39.     void Update()
  40.     {
  41.         IsGrounded = controller.isGrounded;
  42.  
  43.  
  44.         if (lerpCrouch)
  45.         {
  46.             crouchTimer += Time.deltaTime;
  47.             float p = crouchTimer / 1;
  48.             p *= p;
  49.             if (crouching)
  50.                 controller.height = Mathf.Lerp(controller.height, 1, p);
  51.             else
  52.                 controller.height = Mathf.Lerp(controller.height, 2, p);
  53.  
  54.             if(p > 1)
  55.             {
  56.                 lerpCrouch = false;
  57.                 crouchTimer = 0f;
  58.             }
  59.            
  60.         }
  61.  
  62.     }
  63.     // receive inputs for out inputmanager and apply them to the character controller
  64.     public void ProcessMove(Vector2 input)
  65.     {
  66.         Vector3 moveDirection = Vector3.zero;
  67.         moveDirection.x = input.x;
  68.         moveDirection.z = input.y;
  69.         controller.Move(transform.TransformDirection(moveDirection) * speed * Time.deltaTime);
  70.         playerVelocity.y += gravity * Time.deltaTime;
  71.         if (IsGrounded && playerVelocity.y < 0)
  72.             playerVelocity.y = -2f;
  73.         controller.Move(playerVelocity * Time.deltaTime);
  74.         //Debug.Log(playerVelocity.y);
  75.     }
  76.  
  77.     public void Jump()
  78.     {
  79.         if (IsGrounded)
  80.         {
  81.             playerVelocity.y = Mathf.Sqrt(jumpHeight * -3.0f * gravity);
  82.         }
  83.     }
  84.  
  85.     public void Crouch()
  86.     {
  87.         crouching = !crouching;
  88.         crouchTimer = 0;
  89.         lerpCrouch = true;
  90.         if (crouching)
  91.             speed = 3;
  92.         else
  93.             speed = 5;
  94.        
  95.     }
  96.  
  97.     public void ToggleSprint()
  98.     {
  99.         isSprinting = !isSprinting;
  100.  
  101.         myCam.fieldOfView = isSprinting ? runFOV : walkFOV;
  102.     }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment