Advertisement
byIcee

Untitled

Jun 28th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.47 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour {
  6.  
  7.     // TODO:
  8.     // - Arms, Legs
  9.     // - Sliding
  10.     // - Camera Y Offset on sliding
  11.  
  12.     #region Movement Speed Variables
  13.     private float baseMovementSpeed = 1.5f;
  14.     private float runningMovementSpeed = 3.5f;
  15.     private float currentMovementSpeed;
  16.     public enum MovementSpeeds {
  17.         Base,
  18.         Running,
  19.         Current
  20.     }
  21.     public float get_MovementSpeed(MovementSpeeds MovementSpeed) {
  22.         switch (MovementSpeed) {
  23.             case MovementSpeeds.Base:
  24.                 return baseMovementSpeed;
  25.             case MovementSpeeds.Running:
  26.                 return runningMovementSpeed;
  27.             case MovementSpeeds.Current:
  28.                 return currentMovementSpeed;
  29.             default:
  30.                 Debug.LogError("GetCameraFOV wrong enumerator pick");
  31.                 return baseMovementSpeed;
  32.         }
  33.     }
  34.     #endregion
  35.  
  36.     #region Mouse Sensitivity Variables
  37.     private float currentMouseSensitivity;
  38.     private float baseMouseSensitivity = 2f;
  39.     private float SlidingMouseSensitivity;
  40.     public enum MouseSensitivities {
  41.         Base,
  42.         Sliding
  43.     }
  44.     public float? GetMouseSensitivity(MouseSensitivities _ms) {
  45.         switch (_ms) {
  46.             case MouseSensitivities.Base:
  47.                 return baseMouseSensitivity;
  48.             case MouseSensitivities.Sliding:
  49.                 return SlidingMouseSensitivity;
  50.             default:
  51.                 return null;
  52.         }
  53.     }
  54.     #endregion
  55.  
  56.     #region Jump variables
  57.     private const float baseJumpForce = 5f;
  58.     private float currentJumpForce = baseJumpForce;
  59.     #endregion
  60.  
  61.     #region isGrounded
  62.     private bool previous_isGrounded;
  63.     private bool _isGrounded;
  64.     private bool isGrounded {
  65.         get {
  66.             return _isGrounded;
  67.         }
  68.         set {
  69.             _isGrounded = value;
  70.  
  71.             if (previous_isGrounded != value) {
  72.  
  73.                 if (isGrounded) {
  74.                     canJump = true;
  75.                     canMoveHorizontal = true;
  76.                 } else {
  77.                     canJump = false;
  78.                     canMoveHorizontal = false;
  79.                 }
  80.  
  81.             }
  82.  
  83.             previous_isGrounded = value;
  84.         }
  85.     }
  86.     #endregion
  87.  
  88.     #region can/is variables
  89.     [HideInInspector]
  90.     public bool isMoving = false;
  91.     [HideInInspector]
  92.     public bool canMoveHorizontal = true;
  93.     [HideInInspector]
  94.     public bool canMoveVertical = true;
  95.     [HideInInspector]
  96.     public bool canLook = true;
  97.     [HideInInspector]
  98.     public bool canJump = true;
  99.     #endregion
  100.  
  101.     #region Camera Variables
  102.     private const float BaseCameraFOV = 70f;
  103.     private const float RunningCameraFOV = BaseCameraFOV + 30f;
  104.     private float _currentcamerafov = BaseCameraFOV;
  105.     private float CurrentCameraFOV {
  106.         get {
  107.             return _currentcamerafov;
  108.         }
  109.         set {
  110.             _currentcamerafov = value;
  111.             PlayerCamera.fieldOfView = _currentcamerafov;
  112.         }
  113.     }
  114.     public enum CameraFOVs {
  115.         Current,
  116.         Base,
  117.         Running
  118.     }
  119.     public float get_CameraFOV(CameraFOVs CameraFOV) {
  120.         switch (CameraFOV) {
  121.             case CameraFOVs.Current:
  122.                 return CurrentCameraFOV;
  123.             case CameraFOVs.Base:
  124.                 return BaseCameraFOV;
  125.             case CameraFOVs.Running:
  126.                 return RunningCameraFOV;
  127.             default:
  128.                 Debug.LogError("GetCameraFOV wrong enumerator pick");
  129.                 return BaseCameraFOV;
  130.         }
  131.     }
  132.  
  133.     public Vector3 StandingCameraPosition;
  134.     public Vector3 SlidingCameraPosition;
  135.  
  136.     #endregion
  137.  
  138.     #region private Vector3s
  139.     private Vector3 velocity = Vector3.zero;
  140.     private Vector3 rotation = Vector3.zero;
  141.     private Vector3 cameraRotation = Vector3.zero;
  142.     #endregion
  143.  
  144.     #region Collider Variables
  145.     public Collider StandingPlayerCollider;
  146.     public Collider SlidingPlayerCollider;
  147.     private Collider _currentPlayerCollider;
  148.     private Collider CurrentPlayerCollider {
  149.         get {
  150.             return _currentPlayerCollider;
  151.         }
  152.         set {
  153.             _currentPlayerCollider = value;
  154.  
  155.             if (_currentPlayerCollider == StandingPlayerCollider) {
  156.                 StandingPlayerCollider.gameObject.SetActive(true);
  157.                 SlidingPlayerCollider.gameObject.SetActive(false);
  158.             } else if (_currentPlayerCollider == SlidingPlayerCollider) {
  159.                 StandingPlayerCollider.gameObject.SetActive(false);
  160.                 SlidingPlayerCollider.gameObject.SetActive(true);
  161.             }
  162.  
  163.         }
  164.     }
  165.     public enum PlayerColliders {
  166.         Standing,
  167.         Sliding
  168.     }
  169.     public Collider get_PlayerCollider(PlayerColliders PlayerCollider) {
  170.         switch (PlayerCollider) {
  171.             case PlayerColliders.Standing:
  172.                 return StandingPlayerCollider;
  173.             case PlayerColliders.Sliding:
  174.                 return SlidingPlayerCollider;
  175.             default:
  176.                 return null;
  177.         }
  178.     }
  179.     #endregion
  180.  
  181.     #region Inspector variables
  182.     public Camera PlayerCamera;
  183.     public ParticleSystem RunningParticleCollider;
  184.     #endregion
  185.  
  186.     #region Script References
  187.     private RunController m_RunController;
  188.     private SlideController m_SlideController;
  189.     private Rigidbody rb;
  190.     #endregion
  191.  
  192.     private void Awake() {
  193.         rb = GetComponent<Rigidbody>();
  194.         m_SlideController = GetComponent<SlideController>();
  195.         m_RunController = GetComponent<RunController>();
  196.         if (PlayerCamera == null)
  197.             PlayerCamera = GetComponentInChildren<Camera>();
  198.  
  199.         ChangeMouseSensitivity(baseMouseSensitivity);
  200.         ChangeCurrentPlayerCollider(StandingPlayerCollider);
  201.         ChangeCurrentMovementSpeed(baseMovementSpeed);
  202.         ChangeCameraPosition(StandingCameraPosition);
  203.  
  204.         SlidingMouseSensitivity = baseMouseSensitivity / 2;
  205.         distanceToGround = CurrentPlayerCollider.bounds.extents.y;
  206.  
  207.         Cursor.lockState = CursorLockMode.Locked;
  208.     }
  209.  
  210.     private void Update() {
  211.  
  212.         if (canMoveHorizontal || canMoveVertical) {
  213.  
  214.             float HorizontalInput = 0f;
  215.             float VerticalInput = 0f;
  216.  
  217.             if (canMoveHorizontal)
  218.                 HorizontalInput = Input.GetAxisRaw("Horizontal");
  219.  
  220.             if (canMoveVertical)
  221.                 VerticalInput = Input.GetAxisRaw("Vertical");
  222.  
  223.             if (HorizontalInput != 0 || VerticalInput != 0)
  224.                 isMoving = true;
  225.             else
  226.                 isMoving = false;
  227.  
  228.             if (canMoveHorizontal || canMoveVertical) {
  229.  
  230.                 Vector3 HorizontalMovement = transform.right * HorizontalInput;
  231.                 Vector3 VerticalMovement = transform.forward * VerticalInput;
  232.  
  233.                 Vector3 Velocity = (HorizontalMovement + VerticalMovement).normalized * currentMovementSpeed;
  234.                 Move(Velocity);
  235.  
  236.             }
  237.  
  238.         }
  239.  
  240.         if (canLook) {
  241.  
  242.             // Character rotation (turning around (Y Axis))
  243.             float XRotation = Input.GetAxisRaw("Mouse X");
  244.             Vector3 Rotation = new Vector3(0f, XRotation, 0f) * currentMouseSensitivity;
  245.             Rotate(Rotation);
  246.  
  247.             // Camera rotation (up down)
  248.             float YRotation = Input.GetAxisRaw("Mouse Y");
  249.  
  250.             Vector3 CameraRotation = new Vector3(YRotation, 0f, 0f) * currentMouseSensitivity;
  251.             RotateCamera(CameraRotation);
  252.  
  253.         }
  254.  
  255.         //if (CheckIfGrounded())
  256.         //    isGrounded = true;
  257.         //else
  258.         //    isGrounded = false;
  259.  
  260.  
  261.         if (!m_RunController.isRunning && CurrentCameraFOV > BaseCameraFOV)
  262.             m_RunController.RestoreCameraFOV();
  263.  
  264.         #region Running
  265.         if (Input.GetKey(KeyCode.LeftShift) && isMoving) {
  266.             m_RunController.StartRunning();
  267.         }
  268.         if (Input.GetKeyUp(KeyCode.LeftShift)) {
  269.             m_RunController.StopRunning();
  270.         }
  271.         #endregion
  272.  
  273.         #region Jumping
  274.         if (Input.GetKeyDown(KeyCode.Space) && canJump) {
  275.             rb.velocity += new Vector3(0, currentJumpForce, 0);
  276.         }
  277.         #endregion
  278.  
  279.         #region Sliding
  280.         if ((Input.GetKeyDown(KeyCode.LeftControl) || (Input.GetKeyDown(KeyCode.C))) && isMoving && m_RunController.isRunning &&  m_SlideController.canSlide) {
  281.             m_SlideController.StartSliding();
  282.         }
  283.  
  284.         if ((Input.GetKeyUp(KeyCode.LeftControl) || (Input.GetKeyUp(KeyCode.C))) && m_SlideController.isSliding) {
  285.             m_SlideController.StopSliding();
  286.         }
  287.         #endregion
  288.  
  289.     }
  290.  
  291.     private void FixedUpdate() {
  292.         PerformMovement();
  293.         PerformRotation();
  294.     }
  295.  
  296.     private void Move(Vector3 _velocity) {
  297.         velocity = _velocity;
  298.         PerformMovement();
  299.     }
  300.  
  301.     private void PerformMovement() {
  302.         if (velocity != Vector3.zero)
  303.             rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
  304.     }
  305.  
  306.     private void Rotate(Vector3 _rotation) {
  307.         rotation = _rotation;
  308.     }
  309.  
  310.     private void RotateCamera(Vector3 _cameraRotation) {
  311.         cameraRotation = _cameraRotation;
  312.     }
  313.  
  314.     private void PerformRotation() {
  315.         Quaternion TargetRotation = rb.rotation * Quaternion.Euler(rotation);
  316.         rb.MoveRotation(TargetRotation);
  317.         if (PlayerCamera != null)
  318.             PlayerCamera.transform.Rotate(-cameraRotation);
  319.     }
  320.  
  321.     private float distanceToGround;
  322.     private bool CheckIfGrounded() {
  323.         return Physics.Raycast(rb.position, -Vector3.up, distanceToGround + 0.1f);
  324.     }
  325.    
  326.     public void ChangeCameraPosition(Vector3 NewPosition) {
  327.         PlayerCamera.transform.localPosition = NewPosition;
  328.     }
  329.  
  330.     public void ChangeCurrentPlayerCollider(Collider NewPlayerCollider) {
  331.         CurrentPlayerCollider = NewPlayerCollider;
  332.     }
  333.  
  334.     public void ChangeMouseSensitivity(float NewMouseSensitivity) {
  335.         currentMouseSensitivity = NewMouseSensitivity;
  336.     }
  337.  
  338.     public void ChangeCurrentMovementSpeed(float NewMovementSpeed) {
  339.         currentMovementSpeed = NewMovementSpeed;
  340.     }
  341.  
  342.     public void ChangeCurrentCameraFOV(float NewCameraFOV) {
  343.         CurrentCameraFOV = NewCameraFOV;
  344.     }
  345.  
  346.     private void OnCollisionEnter(Collision collision) {
  347.         distanceToGround = CurrentPlayerCollider.bounds.extents.y;
  348.         isGrounded = true;
  349.     }
  350.  
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement