From 89f74273cbffcf91f42a37fcacffc53185a2f327 Mon Sep 17 00:00:00 2001 From: JRiipinen Date: Wed, 16 Oct 2019 22:13:25 +0300 Subject: [PATCH 1/2] Viewpunch and raw mouse input and cleanup code --- Modified fragsurf/PlayerAiming.cs | 150 ++++++++++++++++++------------ 1 file changed, 90 insertions(+), 60 deletions(-) diff --git a/Modified fragsurf/PlayerAiming.cs b/Modified fragsurf/PlayerAiming.cs index 288cb26..e81ec27 100644 --- a/Modified fragsurf/PlayerAiming.cs +++ b/Modified fragsurf/PlayerAiming.cs @@ -1,77 +1,107 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; +using UnityEngine; -public class PlayerAiming : MonoBehaviour { +public class PlayerAiming : MonoBehaviour +{ + [Header("References")] + public Transform bodyTransform; - [Header ("References")] - public Transform bodyTransform = null; + [Header("Sensitivity")] + public float sensitivityMultiplier = 1f; + public float horizontalSensitivity = 1f; + public float verticalSensitivity = 1f; - [Header ("Sensitivity")] - public float sensitivityMultiplier = 1f; - public float horizontalSensitivity = 1f; - public float verticalSensitivity = 1f; + [Header("Restrictions")] + public float minYRotation = -90f; + public float maxYRotation = 90f; - [Header ("Restrictions")] - public float minYRotation = -90f; - public float maxYRotation = 90f; - - // Rotation values - [HideInInspector] public float bodyRotation = 0f; - [HideInInspector] public Vector3 cameraRotation = Vector3.zero; + //The real rotation of the camera without recoil + private Vector3 real_rotation; - private float bodyRotationTemp = 0f; - private Vector3 cameraRotationTemp = Vector3.zero; + [Header("Aimpunch")] + [Tooltip("bigger number makes the response more damped, smaller is less damped, currently the system will overshoot, with larger damping values it won't")] + public float punchDamping = 9.0f; - // Leaning - [HideInInspector] public float leanInput = 0f; + [Tooltip("bigger number increases the speed at which the view corrects")] + public float punchSpringConstant = 65.0f; - // Sway - [HideInInspector] public float sway = 0f; + [HideInInspector] + public Vector2 punchAngle; - void Start () { - - // Lock the mouse - Cursor.lockState = CursorLockMode.Locked; - Cursor.visible = false; + [HideInInspector] + public Vector2 punchAngleVel; - } + private void Start() + { + // Lock the mouse + Cursor.lockState = CursorLockMode.Locked; + Cursor.visible = false; + } - void Update () { - - Vector3 eulerAngles = transform.localEulerAngles; + private void Update() + { + // Fix pausing + if (Mathf.Abs(Time.timeScale) <= 0) + return; - // Remove previous rotation - eulerAngles = new Vector3 (eulerAngles.x - cameraRotationTemp.x, eulerAngles.y, eulerAngles.z - cameraRotationTemp.z); - bodyTransform.eulerAngles -= cameraRotationTemp.y * Vector3.up; - - // Fix pausing - if (Time.timeScale == 0f) - return; + DecayPunchAngle(); - // Input - float xMovement = Input.GetAxis ("Mouse X") * horizontalSensitivity * sensitivityMultiplier; - float yMovement = -Input.GetAxis ("Mouse Y") * verticalSensitivity * sensitivityMultiplier; - - // Rotate camera - cameraRotation = new Vector3 (Mathf.Clamp (cameraRotation.x + yMovement, minYRotation, maxYRotation), - cameraRotation.y + xMovement, - cameraRotation.z + sway); + // Input + float x_movement = Input.GetAxisRaw("Mouse X") * horizontalSensitivity * sensitivityMultiplier; + float y_movement = -Input.GetAxisRaw("Mouse Y") * verticalSensitivity * sensitivityMultiplier; - cameraRotation.z = Mathf.Lerp (cameraRotation.z, 0f, Time.deltaTime * 3f); + // 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); - // Apply rotation - Vector3 clampedRotation = new Vector3 (Mathf.Clamp (cameraRotation.x, minYRotation, maxYRotation), - cameraRotation.y, - cameraRotation.z); + //Apply real rotation to body + bodyTransform.eulerAngles = Vector3.Scale(real_rotation, new Vector3(0f, 1f, 0f)); - eulerAngles = new Vector3 (eulerAngles.x + clampedRotation.x, eulerAngles.y, eulerAngles.z + clampedRotation.z); - bodyTransform.eulerAngles += Vector3.Scale (clampedRotation, new Vector3 (0f, 1f, 0f)); - cameraRotationTemp = clampedRotation; - - // Remove recoil - transform.localEulerAngles = eulerAngles; + var aim_transform = transform; - } + //Apply real rotation to aim + aim_transform.eulerAngles = real_rotation; -} + //Apply recoil + { + //If you want the recoil to be purely visual, move it into LateUpdate + var camera_rotation = aim_transform; + + Vector3 camera_euler_punch_applied = camera_rotation.eulerAngles; + camera_euler_punch_applied.x += punchAngle.x; + camera_euler_punch_applied.y += punchAngle.y; + + camera_rotation.eulerAngles = camera_euler_punch_applied; + } + } + + public void ViewPunch(Vector2 punch_amount) + { + //Remove previous recoil + punchAngle = Vector2.zero; + + //Recoil go up + 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; + } + } +} \ No newline at end of file From edbbf82bffae340adab80bdec17a794e24b93e5a Mon Sep 17 00:00:00 2001 From: JRiipinen Date: Thu, 17 Oct 2019 11:38:20 +0300 Subject: [PATCH 2/2] Further sijmplify and cleanup code --- Modified fragsurf/PlayerAiming.cs | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/Modified fragsurf/PlayerAiming.cs b/Modified fragsurf/PlayerAiming.cs index e81ec27..032b05d 100644 --- a/Modified fragsurf/PlayerAiming.cs +++ b/Modified fragsurf/PlayerAiming.cs @@ -56,22 +56,12 @@ public class PlayerAiming : MonoBehaviour //Apply real rotation to body bodyTransform.eulerAngles = Vector3.Scale(real_rotation, new Vector3(0f, 1f, 0f)); - var aim_transform = transform; + //Apply rotation and recoil + Vector3 camera_euler_punch_applied = real_rotation; + camera_euler_punch_applied.x += punchAngle.x; + camera_euler_punch_applied.y += punchAngle.y; - //Apply real rotation to aim - aim_transform.eulerAngles = real_rotation; - - //Apply recoil - { - //If you want the recoil to be purely visual, move it into LateUpdate - var camera_rotation = aim_transform; - - Vector3 camera_euler_punch_applied = camera_rotation.eulerAngles; - camera_euler_punch_applied.x += punchAngle.x; - camera_euler_punch_applied.y += punchAngle.y; - - camera_rotation.eulerAngles = camera_euler_punch_applied; - } + transform.eulerAngles = camera_euler_punch_applied; } public void ViewPunch(Vector2 punch_amount)