diff options
author | chai <215380520@qq.com> | 2024-03-14 11:43:40 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2024-03-14 11:43:40 +0800 |
commit | cc55520a19043a7b4870858e962fa3e20c46bc39 (patch) | |
tree | b437f788e506a48ec16a215c6965b8170f15d5f6 /_Camera | |
parent | 54c872fa42b1ba0fdbcfe812b80bb8eb0cfe108f (diff) |
*misc
Diffstat (limited to '_Camera')
-rw-r--r-- | _Camera/CameraMovement.cs | 136 | ||||
-rw-r--r-- | _Camera/CameraRecoil.cs | 49 | ||||
-rw-r--r-- | _Camera/CameraWobble.cs | 18 | ||||
-rw-r--r-- | _Camera/WobbleShake.cs | 50 |
4 files changed, 253 insertions, 0 deletions
diff --git a/_Camera/CameraMovement.cs b/_Camera/CameraMovement.cs new file mode 100644 index 0000000..a94012a --- /dev/null +++ b/_Camera/CameraMovement.cs @@ -0,0 +1,136 @@ +using UnityEngine; + +public class CameraMovement : MonoBehaviour +{ + public Camera camera; + + public Transform positionTarget; + + public Transform headPosition; + + public Transform rotationTarget; + + public Transform bobberTarget; + + private StandingDataHandler standingData; + + private float fallingValue; + + private float fallingEffectValue; + + private float minBobble = 0.02f; + + public bool ADS; + + public DamageEffects effects; + + private WeaponHandler weaponHandler; + + private Rigidbody hip; + + private HasControl hasControl; + + private HeadCollisionHandler headCollisionHandler; + + private MovementDataHandler movementData; + + private Rigidbody torso; + + private PlayerDeath death; + + private Strength str; + + private Vector3 cameraCurrentRelativeADSPosition = Vector3.zero; + + private Vector3 movementADSVelocity = Vector3.zero; + + private Vector3 ADSMovementPosition = Vector3.zero; + + private void Start() + { + torso = base.transform.root.GetComponentInChildren<Torso>().GetComponent<Rigidbody>(); + headCollisionHandler = base.transform.root.GetComponentInChildren<HeadCollisionHandler>(); + standingData = base.transform.GetComponentInParent<StandingDataHandler>(); + movementData = base.transform.GetComponentInParent<MovementDataHandler>(); + weaponHandler = base.transform.GetComponentInParent<WeaponHandler>(); + camera = GetComponentInChildren<Camera>(); + hip = base.transform.root.GetComponentInChildren<Hip>().GetComponent<Rigidbody>(); + hasControl = base.transform.root.GetComponent<HasControl>(); + death = base.transform.root.GetComponent<PlayerDeath>(); + str = base.transform.root.GetComponent<Strength>(); + effects = base.transform.root.GetComponent<DamageEffects>(); + } + + private void LateUpdate() + { + if (hasControl.hasControl) + { + headCollisionHandler.collisionValue = 0f; + if (standingData.sinceLanded > 1f) + { + fallingValue = Mathf.Clamp(standingData.sinceGrounded - 0.5f, 0f, 10f) * 0.5f; + } + if (standingData.sinceGrounded > 0.5f || standingData.sinceLanded < 0.5f) + { + fallingEffectValue = Mathf.Lerp(fallingEffectValue, fallingValue, Time.smoothDeltaTime * 15f); + } + else + { + fallingEffectValue = Mathf.Lerp(fallingEffectValue, minBobble, Time.smoothDeltaTime * 3f); + } + float physicsValue = GetPhysicsValue(); + Vector3 vector = Vector3.Lerp(positionTarget.position, bobberTarget.position, Mathf.Clamp(physicsValue + headCollisionHandler.collisionValue, 0f, 1f)); + Vector3 position = vector; + position.y = base.transform.position.y; + base.transform.position = position; + base.transform.position = vector; + base.transform.rotation = Quaternion.Lerp(rotationTarget.rotation, bobberTarget.rotation, Mathf.Clamp(physicsValue + Mathf.Clamp(headCollisionHandler.collisionValue, 0f, 0.4f), 0f, 1f)); + SetCameraPosition(); + } + } + + private void SetCameraPosition() + { + if (ADS && (bool)weaponHandler.gunADS) + { + cameraCurrentRelativeADSPosition = Vector3.Lerp(cameraCurrentRelativeADSPosition, Vector3.zero, Time.deltaTime * 20f); + camera.transform.position = weaponHandler.gunADS.TransformPoint(cameraCurrentRelativeADSPosition) + ADSMovementPosition; + camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, weaponHandler.ADSFOV, Time.deltaTime * 20f); + return; + } + if ((bool)weaponHandler && (bool)weaponHandler.gunADS) + { + cameraCurrentRelativeADSPosition = weaponHandler.gunADS.InverseTransformPoint(camera.transform.position); + } + camera.transform.localPosition = Vector3.Lerp(camera.transform.localPosition, Vector3.zero, Time.deltaTime * 20f); + camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, 90f, Time.deltaTime * 20f); + } + + private void FixedUpdate() + { + if (hasControl.hasControl && (bool)weaponHandler && (bool)weaponHandler.rightGun) + { + movementADSVelocity += (weaponHandler.rightGun.rig.velocity - hip.velocity) * 0.01f; + movementADSVelocity += -ADSMovementPosition * 3f; + movementADSVelocity *= 0.8f; + } + } + + private void Update() + { + if (hasControl.hasControl) + { + ADSMovementPosition += movementADSVelocity * Time.deltaTime; + } + } + + private float GetPhysicsValue() + { + float result = fallingEffectValue * 0.3f + (1f - str.strength) + effects.damageValue; + if (death.dead) + { + result = 1f; + } + return result; + } +} diff --git a/_Camera/CameraRecoil.cs b/_Camera/CameraRecoil.cs new file mode 100644 index 0000000..e82532a --- /dev/null +++ b/_Camera/CameraRecoil.cs @@ -0,0 +1,49 @@ +using UnityEngine; + +public class CameraRecoil : MonoBehaviour +{ + public Transform yRotation; + + public Transform xRotation; + + private float counter; + + private float lastValue; + + private AnimationCurve usedCurve; + + private float deltaValue; + + private float xRecoil; + + private float yRecoil; + + private bool hasCurve; + + private void Start() + { + } + + private void Update() + { + if (hasCurve && counter < usedCurve.keys[usedCurve.length - 1].time) + { + float num = usedCurve.Evaluate(counter); + counter += Time.deltaTime; + deltaValue = num - lastValue; + lastValue = num; + xRotation.Rotate(Vector3.up * Time.deltaTime * xRecoil * deltaValue, Space.World); + yRotation.Rotate(Vector3.right * Time.deltaTime * yRecoil * deltaValue, Space.Self); + } + } + + public void AddRecoil(Vector2 recoil, AnimationCurve curve) + { + counter = 0f; + lastValue = curve.Evaluate(0f); + usedCurve = curve; + xRecoil = recoil.x * 30f; + yRecoil = recoil.y * -30f; + hasCurve = true; + } +} diff --git a/_Camera/CameraWobble.cs b/_Camera/CameraWobble.cs new file mode 100644 index 0000000..2bd1236 --- /dev/null +++ b/_Camera/CameraWobble.cs @@ -0,0 +1,18 @@ +using UnityEngine; + +public class CameraWobble : MonoBehaviour +{ + public Transform target; + + private void Start() + { + } + + private void LateUpdate() + { + if ((bool)target) + { + base.transform.localRotation = target.rotation; + } + } +} diff --git a/_Camera/WobbleShake.cs b/_Camera/WobbleShake.cs new file mode 100644 index 0000000..4da2d38 --- /dev/null +++ b/_Camera/WobbleShake.cs @@ -0,0 +1,50 @@ +using UnityEngine; + +public class WobbleShake : MonoBehaviour +{ + public Vector3 velocity = Vector3.zero; + + public float friction = 0.9f; + + public float movementMultiplier = 10f; + + private Camera mainCam; + + private void Start() + { + mainCam = base.transform.root.GetComponentInChildren<Camera>(); + } + + private void FixedUpdate() + { + Vector3 forward = Vector3.forward; + Vector3 vector = Vector3.Cross(base.transform.forward, forward).normalized * Vector3.Angle(base.transform.forward, forward); + forward = Vector3.up; + Vector3 vector2 = Vector3.Cross(base.transform.up, forward).normalized * Vector3.Angle(base.transform.up, forward); + velocity += (vector + vector2) * movementMultiplier; + velocity *= friction; + base.transform.Rotate(velocity, Space.World); + friction = Mathf.Lerp(friction, 0.7f, Time.fixedDeltaTime * 1f); + } + + public void AddShake(Vector3 shake, float newFriction) + { + Vector3 vector = new Vector3(0f - shake.y, shake.x, shake.z); + if (Mathf.Abs(newFriction - 0.7f) > Mathf.Abs(friction - 0.7f)) + { + friction = newFriction; + } + velocity += vector; + } + + public void AddShakeWorld(Vector3 shake, float newFriction) + { + if ((bool)mainCam) + { + shake = mainCam.transform.TransformDirection(shake); + Vector3 vector = new Vector3(0f - Mathf.Abs(shake.z + shake.y), 0f - shake.x, 0f); + friction = newFriction; + velocity += vector; + } + } +} |