Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerController : MonoBehaviour {
- #region Singleton
- public static PlayerController instance;
- void Awake()
- {
- instance = this;
- }
- #endregion
- #region Variables
- // Use this for initialization
- public float XSensitivity;
- public float YSensitivity;
- public bool clampVerticalRotation = true;
- public Camera fpsCamera;
- private CrosshairController crosshairScript = CrosshairController.instance;
- // why the fuck would this be public
- private static float MinimumX = -90f;
- private static float MaximumX = 90f;
- [SerializeField]
- private int ClimbingAngle = 90;
- [SerializeField]
- private int ClimbingAngleMOE = 10;
- // internal private variables
- private Quaternion m_CharacterTargetRot;
- private Quaternion m_CameraTargetRot;
- private Transform character;
- //private Transform cameraTransform;
- [SerializeField]
- private bool climbing = false;
- [SerializeField]
- private bool onFloor = false;
- private Rigidbody rb;
- //for confirming if the cursor needs to be locked or unlocked
- private bool cursorIsLocked;
- public bool canMove;
- //speed doesn't need to be public either
- [SerializeField]
- private float speed = 1;
- [SerializeField]
- private static float climbSpeed = 0.33f;
- #endregion
- void Start() {
- crosshairScript = FindObjectOfType<CrosshairController>();
- rb = GetComponent<Rigidbody> ();
- // start the game with the cursor locked
- cursorIsLocked = (true);
- LockCursor (true);
- // get a reference to the character's transform (which this script should be attached to)
- character = gameObject.transform;
- // get the location rotation of the character and the camera
- m_CharacterTargetRot = character.localRotation;
- m_CameraTargetRot = fpsCamera.transform.localRotation;
- Settings ();
- }
- public void Settings() {
- XSensitivity = PlayerPrefs.GetFloat ("XSensitivity") * 8;
- YSensitivity = PlayerPrefs.GetFloat ("YSensitivity") * 8;
- fpsCamera.fieldOfView = PlayerPrefs.GetFloat ("FieldOfView", 60);
- }
- #region Updates
- void FixedUpdate() {
- if (cursorIsLocked && canMove) {
- Movement ();
- }
- }
- void Update() {
- if (cursorIsLocked && canMove) {
- LookRotation ();
- }
- }
- #endregion
- #region LockCursor
- public void LockCursor(bool move) {
- // tell the script that the mouse is locked
- cursorIsLocked = (true);
- canMove = move;
- Cursor.visible = false;
- // lock the mouse pointer within the game area
- Cursor.lockState = CursorLockMode.Locked;
- rb.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
- }
- public void UnLockCursor() {
- //this is honestly better as another script instead of having it in the LockCursor script.
- cursorIsLocked = (false);
- canMove = false;
- Cursor.visible = true;
- crosshairScript.SetNone ();
- Cursor.lockState = CursorLockMode.None;
- rb.constraints = RigidbodyConstraints.FreezeRotation;
- }
- #endregion
- void LookRotation()
- {
- //get the y and x rotation based on the Input manager
- float yRot = Input.GetAxis("Mouse X") * XSensitivity;
- float xRot = Input.GetAxis("Mouse Y") * YSensitivity;
- // calculate the rotation
- m_CharacterTargetRot *= Quaternion.Euler (0f, yRot * Time.deltaTime * 100f, 0f);
- m_CameraTargetRot *= Quaternion.Euler (-xRot * Time.deltaTime * 100f, 0f, 0f);
- // clamp the vertical rotation if specified
- if (clampVerticalRotation) {
- m_CameraTargetRot = ClampRotationAroundXAxis(m_CameraTargetRot);
- }
- if (climbing && !onFloor) {
- m_CharacterTargetRot = Quaternion.Euler(0f, ClimbingAngle, 0f);
- }
- character.localRotation = m_CharacterTargetRot;
- fpsCamera.transform.localRotation = m_CameraTargetRot;
- }
- //math
- Quaternion ClampRotationAroundXAxis(Quaternion q)
- {
- q.x /= q.w;
- q.y /= q.w;
- q.z /= q.w;
- q.w = 1.0f;
- float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
- angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);
- q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);
- return q;
- }
- //Now for moving the player
- void Movement() {
- float vertical = Input.GetAxis ("Vertical");
- float horizontal = Input.GetAxis ("Horizontal");
- float movementSpeed;
- Vector3 movement;
- if (climbing) {
- movement.y = vertical;
- movement.x = 0f;
- } else {
- movement.y = 0f;
- movement.x = horizontal;
- }
- if (onFloor) {
- movement.x = horizontal;
- movement.z = vertical;
- movementSpeed = speed;
- } else {
- if (!climbing) {
- movement.y = 1f;
- }
- movement.z = Mathf.Abs (vertical * climbSpeed);
- movementSpeed = speed * climbSpeed;
- }
- transform.Translate (movement * movementSpeed / 100);
- }
- #region OnCollision
- void OnCollisionEnter(Collision collision) {
- if (collision.gameObject.tag == "Floor") {
- onFloor = true;
- }
- if (collision.gameObject.tag == "Ladder") {
- if (transform.rotation.eulerAngles.y > ClimbingAngle - ClimbingAngleMOE && transform.rotation.eulerAngles.y < ClimbingAngle + ClimbingAngleMOE) {
- climbing = true;
- rb.useGravity = false;
- } else {
- rb.AddForce (ClimbingAngle, 0, 0);
- }
- }
- }
- void OnCollisionExit(Collision collision) {
- if (collision.gameObject.tag == "Ladder") {
- climbing = false;
- rb.useGravity = true;
- }
- if (collision.gameObject.tag == "Floor" && climbing) {
- onFloor = false;
- }
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement