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/WobbleShake.cs | |
parent | 54c872fa42b1ba0fdbcfe812b80bb8eb0cfe108f (diff) |
*misc
Diffstat (limited to '_Camera/WobbleShake.cs')
-rw-r--r-- | _Camera/WobbleShake.cs | 50 |
1 files changed, 50 insertions, 0 deletions
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; + } + } +} |