CameraShake.cs 868 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using UnityEngine;
  2. using System.Collections;
  3. public class CameraShake : MonoBehaviour {
  4. //shake part
  5. public const float BaseAmplitude = 0.2f;
  6. public float amplitude = 0;
  7. private float shakeStartTime;
  8. private float shakeDirection;
  9. private SkyBackground skyBg;
  10. public float offsetX = 0;
  11. public float offsetZ = 0;
  12. public void Start()
  13. {
  14. shakeStartTime = GameTime.time;
  15. shakeDirection = Random.Range(0f, 1f)*2*Mathf.PI;
  16. skyBg = GameObject.FindObjectOfType<SkyBackground>();
  17. }
  18. void FixedUpdate()
  19. {
  20. if(amplitude > 0.001f)
  21. {
  22. float dis = amplitude*Mathf.Sin((GameTime.time-shakeStartTime)*50);
  23. amplitude *= 0.95f;
  24. offsetX = dis*Mathf.Cos(shakeDirection);
  25. offsetZ = dis*Mathf.Sin(shakeDirection);
  26. if(skyBg != null)
  27. {
  28. skyBg.Update();
  29. }
  30. }
  31. else
  32. {
  33. offsetX = 0;
  34. offsetZ = 0;
  35. this.enabled = false;
  36. }
  37. }
  38. }