Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CameraController : MonoBehaviour {
  6.  
  7.     Vector2 mouseCam;
  8.     Vector2 smoothVect;
  9.  
  10.     public float sensitivity = 5f;
  11.     public float smoothing = 2f;
  12.  
  13.     public bool camLock = false;
  14.  
  15.     public GameObject Player;
  16.  
  17.     public GameControls input;
  18.  
  19.     public Vector2 deltaInput;
  20.  
  21.     void Awake()
  22.     {
  23.         input = new GameControls();
  24.  
  25.         input.InGame.Look.performed += ctx => deltaInput = ctx.ReadValue<Vector2>();
  26.     }
  27.  
  28.     void Start ()
  29.     {
  30.         Cursor.lockState = CursorLockMode.Locked;
  31.     }
  32.  
  33.     // Update is called once per frame
  34.     void Update ()
  35.     {
  36.         Debug.Log("Mouse Delta: " + deltaInput);
  37.  
  38.         //Old code
  39.  
  40.         /*if (!camLock)
  41.         {
  42.             var mouseDir = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
  43.  
  44.             mouseDir = Vector2.Scale(mouseDir, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
  45.  
  46.             smoothVect.x = Mathf.Lerp(smoothVect.x, mouseDir.x, 1f / smoothing);
  47.             smoothVect.y = Mathf.Lerp(smoothVect.y, mouseDir.y, 1f / smoothing);
  48.  
  49.             mouseCam += smoothVect;
  50.  
  51.             mouseCam.y = Mathf.Clamp(mouseCam.y, -66, 66);
  52.  
  53.             transform.localRotation = Quaternion.AngleAxis(-mouseCam.y, Vector3.right);
  54.             Player.transform.localRotation = Quaternion.AngleAxis(mouseCam.x, Player.transform.up);
  55.  
  56.             if(recoil > 0f)
  57.             {
  58.                 recoil -= (Time.deltaTime * 2);
  59.             }
  60.             else
  61.             {
  62.                 recoil = 0f;
  63.             }
  64.         }*/
  65.  
  66.         if (!camLock)
  67.         {
  68.  
  69.  
  70.             //mouseDir = Vector2.Scale(mouseDir, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
  71.  
  72.             /*smoothVect.x = Mathf.Lerp(smoothVect.x, mouseDir.x, 1f / smoothing);
  73.             smoothVect.y = Mathf.Lerp(smoothVect.y, mouseDir.y, 1f / smoothing);
  74.  
  75.             mouseCam += smoothVect;*/
  76.  
  77.             //mouseCam.y = Mathf.Clamp(mouseCam.y, -66, 66);
  78.  
  79.             Vector2 fullRot = new Vector2(deltaInput.y, 0) * Time.deltaTime;
  80.             Vector2 selfRot = new Vector2(0, deltaInput.x) * Time.deltaTime;
  81.  
  82.             /*transform.localRotation = Quaternion.AngleAxis(-deltaInput.y, Vector3.right);
  83.             Player.transform.localRotation = Quaternion.AngleAxis(deltaInput.x, Player.transform.up);*/
  84.  
  85.             transform.Rotate(selfRot);
  86.             Player.transform.Rotate(fullRot);
  87.  
  88.             if (recoil > 0f)
  89.             {
  90.                 recoil -= (Time.deltaTime * 2);
  91.             }
  92.             else
  93.             {
  94.                 recoil = 0f;
  95.             }
  96.         }
  97.     }
  98.  
  99.     void OnEnable()
  100.     {
  101.         input.Enable();
  102.     }
  103.  
  104.     void OnDisable()
  105.     {
  106.         input.Disable();
  107.     }
  108.  
  109.     private float recoil = 0.0f;
  110.     public void StartRecoil(float recoilFeedthrough)
  111.     {
  112.         recoil = recoilFeedthrough;
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement