Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.36 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerController : MonoBehaviour {
  5.  
  6.     #region Singleton
  7.  
  8.     public static PlayerController instance;
  9.  
  10.     void Awake()
  11.     {
  12.         instance = this;
  13.     }
  14.  
  15.     #endregion
  16.  
  17.     #region Variables
  18.     // Use this for initialization
  19.     public float XSensitivity;
  20.     public float YSensitivity;
  21.     public bool clampVerticalRotation = true;
  22.     public Camera fpsCamera;
  23.     private CrosshairController crosshairScript = CrosshairController.instance;
  24.  
  25.     // why the fuck would this be public
  26.     private static float MinimumX = -90f;
  27.     private static float MaximumX = 90f;
  28.     [SerializeField]
  29.     private int ClimbingAngle = 90;
  30.     [SerializeField]
  31.     private int ClimbingAngleMOE = 10;
  32.  
  33.     // internal private variables
  34.     private Quaternion m_CharacterTargetRot;
  35.     private Quaternion m_CameraTargetRot;
  36.     private Transform character;
  37.     //private Transform cameraTransform;
  38.  
  39.  
  40.     [SerializeField]
  41.     private bool climbing = false;
  42.     [SerializeField]
  43.     private bool onFloor = false;
  44.     private Rigidbody rb;
  45.  
  46.     //for confirming if the cursor needs to be locked or unlocked
  47.     private bool cursorIsLocked;
  48.  
  49.     public bool canMove;
  50.  
  51.     //speed doesn't need to be public either
  52.     [SerializeField]
  53.     private float speed = 1;
  54.     [SerializeField]
  55.     private static float climbSpeed = 0.33f;
  56.     #endregion
  57.  
  58.     void Start() {
  59.         crosshairScript = FindObjectOfType<CrosshairController>();
  60.  
  61.         rb = GetComponent<Rigidbody> ();
  62.  
  63.         // start the game with the cursor locked
  64.         cursorIsLocked = (true);
  65.         LockCursor (true);
  66.  
  67.         // get a reference to the character's transform (which this script should be attached to)
  68.         character = gameObject.transform;
  69.  
  70.         // get the location rotation of the character and the camera
  71.         m_CharacterTargetRot = character.localRotation;
  72.         m_CameraTargetRot = fpsCamera.transform.localRotation;
  73.  
  74.         Settings ();
  75.     }
  76.  
  77.     public void Settings() {
  78.         XSensitivity = PlayerPrefs.GetFloat ("XSensitivity") * 8;
  79.         YSensitivity = PlayerPrefs.GetFloat ("YSensitivity") * 8;
  80.         fpsCamera.fieldOfView = PlayerPrefs.GetFloat ("FieldOfView", 60);
  81.     }
  82.  
  83.     #region Updates
  84.     void FixedUpdate() {
  85.         if (cursorIsLocked && canMove) {
  86.             Movement ();
  87.         }
  88.     }
  89.  
  90.     void Update() {
  91.         if (cursorIsLocked && canMove) {
  92.             LookRotation ();
  93.         }
  94.     }
  95.     #endregion
  96.  
  97.     #region LockCursor
  98.     public void LockCursor(bool move) {
  99.         // tell the script that the mouse is locked
  100.         cursorIsLocked = (true);
  101.         canMove = move;
  102.         Cursor.visible = false;
  103.  
  104.         // lock the mouse pointer within the game area
  105.         Cursor.lockState = CursorLockMode.Locked;
  106.         rb.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
  107.     }
  108.  
  109.     public void UnLockCursor() {
  110.         //this is honestly better as another script instead of having it in the LockCursor script.
  111.         cursorIsLocked = (false);
  112.         canMove = false;
  113.         Cursor.visible = true;
  114.         crosshairScript.SetNone ();
  115.  
  116.         Cursor.lockState = CursorLockMode.None;
  117.         rb.constraints = RigidbodyConstraints.FreezeRotation;
  118.     }
  119.     #endregion
  120.  
  121.     void LookRotation()
  122.     {
  123.         //get the y and x rotation based on the Input manager
  124.         float yRot = Input.GetAxis("Mouse X") * XSensitivity;
  125.         float xRot = Input.GetAxis("Mouse Y") * YSensitivity;
  126.  
  127.         // calculate the rotation
  128.         m_CharacterTargetRot *= Quaternion.Euler (0f, yRot * Time.deltaTime * 100f, 0f);
  129.         m_CameraTargetRot *= Quaternion.Euler (-xRot * Time.deltaTime * 100f, 0f, 0f);
  130.  
  131.         // clamp the vertical rotation if specified
  132.         if (clampVerticalRotation) {
  133.             m_CameraTargetRot = ClampRotationAroundXAxis(m_CameraTargetRot);
  134.         }
  135.            
  136.         if (climbing && !onFloor) {
  137.             m_CharacterTargetRot = Quaternion.Euler(0f, ClimbingAngle, 0f);
  138.         }
  139.         character.localRotation = m_CharacterTargetRot;
  140.         fpsCamera.transform.localRotation = m_CameraTargetRot;
  141.     }
  142.    
  143.     //math
  144.     Quaternion ClampRotationAroundXAxis(Quaternion q)
  145.     {
  146.         q.x /= q.w;
  147.         q.y /= q.w;
  148.         q.z /= q.w;
  149.         q.w = 1.0f;
  150.  
  151.         float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
  152.  
  153.         angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);
  154.  
  155.         q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);
  156.  
  157.         return q;
  158.     }
  159.    
  160.     //Now for moving the player
  161.     void Movement() {
  162.        
  163.         float vertical = Input.GetAxis ("Vertical");
  164.         float horizontal = Input.GetAxis ("Horizontal");
  165.         float movementSpeed;
  166.         Vector3 movement;
  167.  
  168.         if (climbing) {
  169.             movement.y = vertical;
  170.             movement.x = 0f;
  171.         } else {
  172.             movement.y = 0f;
  173.             movement.x = horizontal;
  174.         }
  175.         if (onFloor) {
  176.             movement.x = horizontal;
  177.             movement.z = vertical;
  178.             movementSpeed = speed;
  179.         } else {
  180.             if (!climbing) {
  181.                 movement.y = 1f;
  182.             }
  183.             movement.z = Mathf.Abs (vertical * climbSpeed);
  184.             movementSpeed = speed * climbSpeed;
  185.         }
  186.  
  187.         transform.Translate (movement * movementSpeed / 100);
  188.     }
  189.     #region OnCollision
  190.     void OnCollisionEnter(Collision collision) {
  191.         if (collision.gameObject.tag == "Floor") {
  192.             onFloor = true;
  193.         }
  194.  
  195.         if (collision.gameObject.tag == "Ladder") {
  196.             if (transform.rotation.eulerAngles.y > ClimbingAngle - ClimbingAngleMOE && transform.rotation.eulerAngles.y < ClimbingAngle + ClimbingAngleMOE) {
  197.                 climbing = true;
  198.                 rb.useGravity = false;
  199.             } else {
  200.                 rb.AddForce (ClimbingAngle, 0, 0);
  201.             }
  202.         }
  203.     }
  204.  
  205.     void OnCollisionExit(Collision collision) {
  206.         if (collision.gameObject.tag == "Ladder") {
  207.             climbing = false;
  208.             rb.useGravity = true;
  209.         }
  210.  
  211.         if (collision.gameObject.tag == "Floor" && climbing) {
  212.             onFloor = false;
  213.         }
  214.     }
  215.     #endregion
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement