Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3.  
  4. public class PlayerAiming : MonoBehaviour
  5. {
  6.     [Header("References")]
  7.     public Transform bodyTransform;
  8.  
  9.     [Header("Sensitivity")]
  10.     public float sensitivityMultiplier = 1f;
  11.     public float horizontalSensitivity = 1f;
  12.     public float verticalSensitivity   = 1f;
  13.  
  14.     [Header("Restrictions")]
  15.     public float minYRotation = -90f;
  16.     public float maxYRotation = 90f;
  17.  
  18.     //The real rotation of the camera without recoil
  19.     private Vector3 real_rotation;
  20.  
  21.     [Header("Aimpunch")]
  22.     [Tooltip("bigger number makes the response more damped, smaller is less damped, currently the system will overshoot, with larger damping values it won't")]
  23.     public float punchDamping = 9.0f;
  24.  
  25.     [Tooltip("bigger number increases the speed at which the view corrects")]
  26.     public float punchSpringConstant = 65.0f;
  27.  
  28.     [HideInInspector]
  29.     public Vector2 punchAngle;
  30.  
  31.     [HideInInspector]
  32.     public Vector2 punchAngleVel;
  33.  
  34.     private void Start()
  35.     {
  36.         // Lock the mouse
  37.         Cursor.lockState = CursorLockMode.Locked;
  38.         Cursor.visible   = false;
  39.     }
  40.  
  41.     private void Update()
  42.     {
  43.         // Fix pausing
  44.         if (Math.Abs(Time.timeScale) <= 0)
  45.             return;
  46.  
  47.         DecayPunchAngle();
  48.  
  49.         // Input
  50.         float x_movement = Input.GetAxisRaw("Mouse X") * horizontalSensitivity * sensitivityMultiplier;
  51.         float y_movement = -Input.GetAxisRaw("Mouse Y") * verticalSensitivity  * sensitivityMultiplier;
  52.  
  53.         // Calculate real rotation from input
  54.         real_rotation   = new Vector3(Mathf.Clamp(real_rotation.x + y_movement, minYRotation, maxYRotation), real_rotation.y + x_movement, real_rotation.z);
  55.         real_rotation.z = Mathf.Lerp(real_rotation.z, 0f, Time.deltaTime * 3f);
  56.  
  57.         //Apply real rotation to body
  58.         bodyTransform.eulerAngles = Vector3.Scale(real_rotation, new Vector3(0f, 1f, 0f));
  59.  
  60.         var aim_transform = transform;
  61.  
  62.         //Apply real rotation to aim
  63.         aim_transform.eulerAngles = real_rotation;
  64.  
  65.         //Apply recoil
  66.         {
  67.             //If you want the recoil to be purely visual, move it into LateUpdate
  68.             var camera_rotation = aim_transform;
  69.  
  70.             Vector3 camera_euler_punch_applied = camera_rotation.eulerAngles;
  71.             camera_euler_punch_applied.x += punchAngle.x;
  72.             camera_euler_punch_applied.y += punchAngle.y;
  73.  
  74.             camera_rotation.eulerAngles = camera_euler_punch_applied;
  75.         }
  76.     }
  77.  
  78.     public void ViewPunch(Vector2 punch_amount)
  79.     {
  80.         //Remove previous recoil
  81.         punchAngle = Vector2.zero;
  82.  
  83.         //Recoil go up
  84.         punchAngleVel -= punch_amount * 20;
  85.     }
  86.  
  87.     private void DecayPunchAngle()
  88.     {
  89.         if (punchAngle.sqrMagnitude > 0.001 || punchAngleVel.sqrMagnitude > 0.001)
  90.         {
  91.             punchAngle += punchAngleVel * Time.deltaTime;
  92.             float damping = 1 - (punchDamping * Time.deltaTime);
  93.  
  94.             if (damping < 0)
  95.                 damping = 0;
  96.  
  97.             punchAngleVel *= damping;
  98.  
  99.             float spring_force_magnitude = punchSpringConstant * Time.deltaTime;
  100.             punchAngleVel -= punchAngle * spring_force_magnitude;
  101.         }
  102.         else
  103.         {
  104.             punchAngle    = Vector2.zero;
  105.             punchAngleVel = Vector2.zero;
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement