Viewpunch and raw mouse input

and cleanup code
This commit is contained in:
JRiipinen
2019-10-16 22:13:25 +03:00
parent 805c53b797
commit 89f74273cb

View File

@@ -1,77 +1,107 @@
using System.Collections; using UnityEngine;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAiming : MonoBehaviour { public class PlayerAiming : MonoBehaviour
{
[Header("References")]
public Transform bodyTransform;
[Header ("References")] [Header("Sensitivity")]
public Transform bodyTransform = null; public float sensitivityMultiplier = 1f;
public float horizontalSensitivity = 1f;
public float verticalSensitivity = 1f;
[Header ("Sensitivity")] [Header("Restrictions")]
public float sensitivityMultiplier = 1f; public float minYRotation = -90f;
public float horizontalSensitivity = 1f; public float maxYRotation = 90f;
public float verticalSensitivity = 1f;
[Header ("Restrictions")] //The real rotation of the camera without recoil
public float minYRotation = -90f; private Vector3 real_rotation;
public float maxYRotation = 90f;
// Rotation values [Header("Aimpunch")]
[HideInInspector] public float bodyRotation = 0f; [Tooltip("bigger number makes the response more damped, smaller is less damped, currently the system will overshoot, with larger damping values it won't")]
[HideInInspector] public Vector3 cameraRotation = Vector3.zero; public float punchDamping = 9.0f;
private float bodyRotationTemp = 0f; [Tooltip("bigger number increases the speed at which the view corrects")]
private Vector3 cameraRotationTemp = Vector3.zero; public float punchSpringConstant = 65.0f;
// Leaning [HideInInspector]
[HideInInspector] public float leanInput = 0f; public Vector2 punchAngle;
// Sway [HideInInspector]
[HideInInspector] public float sway = 0f; public Vector2 punchAngleVel;
void Start () { private void Start()
{
// Lock the mouse
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Lock the mouse private void Update()
Cursor.lockState = CursorLockMode.Locked; {
Cursor.visible = false; // Fix pausing
if (Mathf.Abs(Time.timeScale) <= 0)
return;
} DecayPunchAngle();
void Update () { // Input
float x_movement = Input.GetAxisRaw("Mouse X") * horizontalSensitivity * sensitivityMultiplier;
float y_movement = -Input.GetAxisRaw("Mouse Y") * verticalSensitivity * sensitivityMultiplier;
Vector3 eulerAngles = transform.localEulerAngles; // Calculate real rotation from input
real_rotation = new Vector3(Mathf.Clamp(real_rotation.x + y_movement, minYRotation, maxYRotation), real_rotation.y + x_movement, real_rotation.z);
real_rotation.z = Mathf.Lerp(real_rotation.z, 0f, Time.deltaTime * 3f);
// Remove previous rotation //Apply real rotation to body
eulerAngles = new Vector3 (eulerAngles.x - cameraRotationTemp.x, eulerAngles.y, eulerAngles.z - cameraRotationTemp.z); bodyTransform.eulerAngles = Vector3.Scale(real_rotation, new Vector3(0f, 1f, 0f));
bodyTransform.eulerAngles -= cameraRotationTemp.y * Vector3.up;
// Fix pausing var aim_transform = transform;
if (Time.timeScale == 0f)
return;
// Input //Apply real rotation to aim
float xMovement = Input.GetAxis ("Mouse X") * horizontalSensitivity * sensitivityMultiplier; aim_transform.eulerAngles = real_rotation;
float yMovement = -Input.GetAxis ("Mouse Y") * verticalSensitivity * sensitivityMultiplier;
// Rotate camera //Apply recoil
cameraRotation = new Vector3 (Mathf.Clamp (cameraRotation.x + yMovement, minYRotation, maxYRotation), {
cameraRotation.y + xMovement, //If you want the recoil to be purely visual, move it into LateUpdate
cameraRotation.z + sway); var camera_rotation = aim_transform;
cameraRotation.z = Mathf.Lerp (cameraRotation.z, 0f, Time.deltaTime * 3f); Vector3 camera_euler_punch_applied = camera_rotation.eulerAngles;
camera_euler_punch_applied.x += punchAngle.x;
camera_euler_punch_applied.y += punchAngle.y;
// Apply rotation camera_rotation.eulerAngles = camera_euler_punch_applied;
Vector3 clampedRotation = new Vector3 (Mathf.Clamp (cameraRotation.x, minYRotation, maxYRotation), }
cameraRotation.y, }
cameraRotation.z);
eulerAngles = new Vector3 (eulerAngles.x + clampedRotation.x, eulerAngles.y, eulerAngles.z + clampedRotation.z); public void ViewPunch(Vector2 punch_amount)
bodyTransform.eulerAngles += Vector3.Scale (clampedRotation, new Vector3 (0f, 1f, 0f)); {
cameraRotationTemp = clampedRotation; //Remove previous recoil
punchAngle = Vector2.zero;
// Remove recoil //Recoil go up
transform.localEulerAngles = eulerAngles; punchAngleVel -= punch_amount * 20;
}
} private void DecayPunchAngle()
{
if (punchAngle.sqrMagnitude > 0.001 || punchAngleVel.sqrMagnitude > 0.001)
{
punchAngle += punchAngleVel * Time.deltaTime;
float damping = 1 - (punchDamping * Time.deltaTime);
if (damping < 0)
damping = 0;
punchAngleVel *= damping;
float spring_force_magnitude = punchSpringConstant * Time.deltaTime;
punchAngleVel -= punchAngle * spring_force_magnitude;
}
else
{
punchAngle = Vector2.zero;
punchAngleVel = Vector2.zero;
}
}
} }