Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Controller : MonoBehaviour {
- CharacterController player;
- public Camera cam;
- public GameObject body;
- public GameObject pauseMenu;
- public float msConversionScalar;
- Vector3 movementVector;
- public float gravity;
- public float walkingSpeed;
- public float sprintingSpeed;
- public float drag;
- public float jumpHeight;
- public float fallDamageThreshold;
- public CursorLockMode lockMode;
- public bool canMove;
- Vector2 mouseAbsolute;
- Vector2 smoothMouse;
- Vector2 targetDirection;
- Vector2 targetCharacterDirection;
- Vector2 clampInDeg = new Vector2(360, 180);
- public float sensitivityValue;
- public float smoothingValue;
- Vector2 sensitivity;
- Vector2 smoothing;
- Vector2 initialVelocity;
- bool falling;
- bool jump;
- bool freeCam;
- bool isSprinting;
- bool menuOpen;
- // Use this for initialization
- void Start () {
- player = this.GetComponent<CharacterController>();
- targetDirection = transform.localRotation.eulerAngles;
- Cursor.lockState = lockMode;
- menuOpen = false;
- }
- // Update is called once per frame
- void Update () {
- sensitivity = new Vector2(sensitivityValue, sensitivityValue);
- smoothing = new Vector2(smoothingValue, smoothingValue);
- ButtonControls();
- if(!menuOpen)
- {
- UpdateMouse();
- }
- UpdatePos();
- }
- void UpdatePos()
- {
- if(player.isGrounded)
- {
- if(falling == true)
- {
- // We were falling last frame
- // We can use this opportunity to check for fall damage and to reset our jump bool
- if(toMetersPerSecond(movementVector.y) < -fallDamageThreshold)
- {
- print("fall damage");
- }
- }
- falling = false;
- movementVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
- movementVector = transform.TransformDirection(movementVector);
- if (isSprinting)
- {
- movementVector *= toUnits(sprintingSpeed);
- }
- else
- {
- movementVector *= toUnits(walkingSpeed);
- }
- if(jump)
- {
- jump = false;
- movementVector.y = toUnits(jumpHeight);
- }
- }
- else
- {
- if (falling == false)
- initialVelocity = new Vector2(player.velocity.x, player.velocity.z);
- falling = true;
- Vector3 inputVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
- inputVector = transform.TransformDirection(inputVector);
- Vector2 clampedHorizontalVelocity =
- Vector2.ClampMagnitude(new Vector2(player.velocity.x, player.velocity.z), initialVelocity.magnitude);
- movementVector =
- new Vector3(clampedHorizontalVelocity.x, player.velocity.y, clampedHorizontalVelocity.y)
- + (inputVector * Time.deltaTime * toUnits(walkingSpeed*1.5f));
- }
- movementVector.y -= toUnits(gravity) * Time.deltaTime;
- print("Vel.x = " + movementVector.x + " Vel.y = " + movementVector.y + " Vel.z = " + movementVector.z + " (" + falling + ")");
- player.Move(movementVector * Time.deltaTime);
- }
- void UpdateMouse()
- {
- Quaternion targetOrientation = Quaternion.Euler(targetDirection);
- Quaternion targetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);
- Vector2 mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
- mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));
- smoothMouse.x = Mathf.Lerp(smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
- smoothMouse.y = Mathf.Lerp(smoothMouse.y, mouseDelta.y, 1f / smoothing.y);
- mouseAbsolute += smoothMouse;
- if (clampInDeg.x < 360)
- {
- mouseAbsolute.x = Mathf.Clamp(mouseAbsolute.x, -clampInDeg.x * 0.5f, clampInDeg.x * 0.5f);
- }
- Quaternion xRot = Quaternion.AngleAxis(-mouseAbsolute.y, targetOrientation * Vector3.right);
- cam.transform.localRotation = xRot;
- if (clampInDeg.y < 360)
- {
- mouseAbsolute.y = Mathf.Clamp(mouseAbsolute.y, -clampInDeg.y * 0.5f, clampInDeg.y * 0.5f);
- }
- cam.transform.localRotation *= targetOrientation;
- if (!freeCam)
- {
- Quaternion yRot = Quaternion.AngleAxis(mouseAbsolute.x, this.transform.up);
- this.transform.localRotation = yRot;
- this.transform.localRotation *= targetCharacterOrientation;
- }
- else
- {
- // This could be written better.
- Quaternion yRot = Quaternion.AngleAxis(mouseAbsolute.x, this.transform.up);
- cam.transform.localRotation = yRot;
- }
- }
- void ButtonControls()
- {
- if (Input.GetButtonDown("Menu"))
- {
- DrawMenu();
- }
- if(!menuOpen)
- {
- if (Input.GetButton("Free Look"))
- {
- freeCam = true;
- }
- else
- {
- freeCam = false;
- }
- if (Input.GetButton("Sprint"))
- {
- isSprinting = true;
- }
- else
- {
- isSprinting = false;
- }
- if (Input.GetButtonDown("Jump") && player.isGrounded)
- {
- jump = true;
- }
- }
- }
- public void DrawMenu()
- {
- if(menuOpen)
- {
- Cursor.lockState = CursorLockMode.Locked;
- pauseMenu.SetActive(false);
- menuOpen = false;
- }
- else if (!menuOpen)
- {
- Cursor.lockState = CursorLockMode.None;
- pauseMenu.SetActive(true);
- menuOpen = true;
- }
- else
- {
- Debug.Log("Menu not set");
- }
- }
- float toMetersPerSecond(float num)
- {
- num = num/msConversionScalar;
- return num;
- }
- float toUnits(float num)
- {
- num = num*msConversionScalar;
- return num;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement