This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| public class CameraShakes : MonoBehaviour | |
| { | |
| public bool Shaking; | |
| private float ShakeDecay; | |
| private float ShakeIntensity; | |
| private Vector3 OriginalPos; | |
| private Quaternion OriginalRot; | |
| void OnEnable () | |
| { | |
| DoShake (); | |
| } | |
| void Start () | |
| { | |
| Shaking = false; | |
| } | |
| // Update is called once per frame | |
| void Update () | |
| { | |
| if (ShakeIntensity > 0) { | |
| transform.position = OriginalPos + Random.insideUnitSphere * ShakeIntensity; | |
| transform.rotation = new Quaternion (OriginalRot.x + Random.Range (-ShakeIntensity, ShakeIntensity) * .2f, | |
| OriginalRot.y + Random.Range (-ShakeIntensity, ShakeIntensity) * .2f, | |
| OriginalRot.z + Random.Range (-ShakeIntensity, ShakeIntensity) * .2f, | |
| OriginalRot.w + Random.Range (-ShakeIntensity, ShakeIntensity) * .2f); | |
| ShakeIntensity -= ShakeDecay; | |
| } else if (Shaking) { | |
| Shaking = false; | |
| } | |
| } | |
| public void DoShake () | |
| { | |
| OriginalPos = transform.position; | |
| OriginalRot = transform.rotation; | |
| ShakeIntensity = 0.3f; | |
| ShakeDecay = 0.02f; | |
| Shaking = true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment