Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.50 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Controller : MonoBehaviour {
  6.  
  7.     CharacterController player;
  8.     public Camera cam;
  9.     public GameObject body;
  10.     public GameObject pauseMenu;
  11.  
  12.     public float msConversionScalar;
  13.  
  14.     Vector3 movementVector;
  15.     public float gravity;
  16.     public float walkingSpeed;
  17.     public float sprintingSpeed;
  18.     public float drag;
  19.     public float jumpHeight;
  20.     public float fallDamageThreshold;
  21.  
  22.     public CursorLockMode lockMode;
  23.     public bool canMove;
  24.  
  25.     Vector2 mouseAbsolute;
  26.     Vector2 smoothMouse;
  27.     Vector2 targetDirection;
  28.     Vector2 targetCharacterDirection;
  29.     Vector2 clampInDeg = new Vector2(360, 180);
  30.     public float sensitivityValue;
  31.     public float smoothingValue;
  32.     Vector2 sensitivity;
  33.     Vector2 smoothing;
  34.  
  35.     Vector2 initialVelocity;
  36.  
  37.     bool falling;
  38.     bool jump;
  39.     bool freeCam;
  40.     bool isSprinting;
  41.     bool menuOpen;
  42.  
  43.     // Use this for initialization
  44.     void Start () {
  45.         player = this.GetComponent<CharacterController>();
  46.         targetDirection = transform.localRotation.eulerAngles;
  47.         Cursor.lockState = lockMode;
  48.         menuOpen = false;
  49.     }
  50.    
  51.     // Update is called once per frame
  52.     void Update () {
  53.         sensitivity = new Vector2(sensitivityValue, sensitivityValue);
  54.         smoothing = new Vector2(smoothingValue, smoothingValue);
  55.         ButtonControls();
  56.         if(!menuOpen)
  57.         {
  58.             UpdateMouse();
  59.         }
  60.         UpdatePos();
  61.     }
  62.  
  63.     void UpdatePos()
  64.     {
  65.         if(player.isGrounded)
  66.         {
  67.             if(falling == true)
  68.             {
  69.                 // We were falling last frame
  70.                 // We can use this opportunity to check for fall damage and to reset our jump bool
  71.                 if(toMetersPerSecond(movementVector.y) < -fallDamageThreshold)
  72.                 {
  73.                     print("fall damage");
  74.                 }
  75.             }
  76.             falling = false;
  77.             movementVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  78.             movementVector = transform.TransformDirection(movementVector);
  79.             if (isSprinting)
  80.             {
  81.                 movementVector *= toUnits(sprintingSpeed);
  82.             }
  83.             else
  84.             {
  85.                 movementVector *= toUnits(walkingSpeed);
  86.             }
  87.             if(jump)
  88.             {
  89.                 jump = false;
  90.                 movementVector.y = toUnits(jumpHeight);
  91.             }
  92.         }
  93.         else
  94.         {
  95.             if (falling == false)
  96.                 initialVelocity = new Vector2(player.velocity.x, player.velocity.z);
  97.             falling = true;
  98.             Vector3 inputVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  99.             inputVector = transform.TransformDirection(inputVector);
  100.             Vector2 clampedHorizontalVelocity =
  101.                 Vector2.ClampMagnitude(new Vector2(player.velocity.x, player.velocity.z), initialVelocity.magnitude);
  102.             movementVector =
  103.                 new Vector3(clampedHorizontalVelocity.x, player.velocity.y, clampedHorizontalVelocity.y)
  104.                 + (inputVector * Time.deltaTime * toUnits(walkingSpeed*1.5f));
  105.         }
  106.  
  107.         movementVector.y -= toUnits(gravity) * Time.deltaTime;
  108.         print("Vel.x = " + movementVector.x + " Vel.y = " + movementVector.y + " Vel.z = " + movementVector.z + " (" + falling + ")");
  109.         player.Move(movementVector * Time.deltaTime);
  110.     }
  111.  
  112.     void UpdateMouse()
  113.     {
  114.         Quaternion targetOrientation = Quaternion.Euler(targetDirection);
  115.         Quaternion targetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);
  116.  
  117.         Vector2 mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
  118.         mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));
  119.  
  120.         smoothMouse.x = Mathf.Lerp(smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
  121.         smoothMouse.y = Mathf.Lerp(smoothMouse.y, mouseDelta.y, 1f / smoothing.y);
  122.  
  123.         mouseAbsolute += smoothMouse;
  124.  
  125.         if (clampInDeg.x < 360)
  126.         {
  127.             mouseAbsolute.x = Mathf.Clamp(mouseAbsolute.x, -clampInDeg.x * 0.5f, clampInDeg.x * 0.5f);
  128.         }
  129.  
  130.         Quaternion xRot = Quaternion.AngleAxis(-mouseAbsolute.y, targetOrientation * Vector3.right);
  131.         cam.transform.localRotation = xRot;
  132.  
  133.         if (clampInDeg.y < 360)
  134.         {
  135.             mouseAbsolute.y = Mathf.Clamp(mouseAbsolute.y, -clampInDeg.y * 0.5f, clampInDeg.y * 0.5f);
  136.         }
  137.  
  138.         cam.transform.localRotation *= targetOrientation;
  139.  
  140.         if (!freeCam)
  141.         {
  142.             Quaternion yRot = Quaternion.AngleAxis(mouseAbsolute.x, this.transform.up);
  143.             this.transform.localRotation = yRot;
  144.             this.transform.localRotation *= targetCharacterOrientation;
  145.         }
  146.         else
  147.         {
  148.             // This could be written better.
  149.             Quaternion yRot = Quaternion.AngleAxis(mouseAbsolute.x, this.transform.up);
  150.             cam.transform.localRotation = yRot;
  151.         }
  152.     }
  153.  
  154.     void ButtonControls()
  155.     {
  156.         if (Input.GetButtonDown("Menu"))
  157.         {
  158.             DrawMenu();
  159.         }
  160.         if(!menuOpen)
  161.         {
  162.             if (Input.GetButton("Free Look"))
  163.             {
  164.                 freeCam = true;
  165.             }
  166.             else
  167.             {
  168.                 freeCam = false;
  169.             }
  170.  
  171.             if (Input.GetButton("Sprint"))
  172.             {
  173.                 isSprinting = true;
  174.             }
  175.             else
  176.             {
  177.                 isSprinting = false;
  178.             }
  179.  
  180.             if (Input.GetButtonDown("Jump") && player.isGrounded)
  181.             {
  182.                 jump = true;
  183.             }
  184.         }
  185.     }
  186.  
  187.     public void DrawMenu()
  188.     {
  189.         if(menuOpen)
  190.         {
  191.             Cursor.lockState = CursorLockMode.Locked;
  192.             pauseMenu.SetActive(false);
  193.             menuOpen = false;
  194.         }
  195.         else if (!menuOpen)
  196.         {
  197.             Cursor.lockState = CursorLockMode.None;
  198.             pauseMenu.SetActive(true);
  199.             menuOpen = true;
  200.         }
  201.         else
  202.         {
  203.             Debug.Log("Menu not set");
  204.         }
  205.     }
  206.  
  207.     float toMetersPerSecond(float num)
  208.     {
  209.         num = num/msConversionScalar;
  210.         return num;
  211.     }
  212.  
  213.     float toUnits(float num)
  214.     {
  215.         num = num*msConversionScalar;
  216.         return num;
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement