123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using UnityEngine;
- using System.Collections;
- public class CameraShake : MonoBehaviour {
- //shake part
- public const float BaseAmplitude = 0.2f;
- public float amplitude = 0;
- private float shakeStartTime;
- private float shakeDirection;
- private SkyBackground skyBg;
- public float offsetX = 0;
- public float offsetZ = 0;
- public void Start()
- {
- shakeStartTime = GameTime.time;
- shakeDirection = Random.Range(0f, 1f)*2*Mathf.PI;
- skyBg = GameObject.FindObjectOfType<SkyBackground>();
- }
-
- void FixedUpdate()
- {
- if(amplitude > 0.001f)
- {
- float dis = amplitude*Mathf.Sin((GameTime.time-shakeStartTime)*50);
- amplitude *= 0.95f;
-
- offsetX = dis*Mathf.Cos(shakeDirection);
- offsetZ = dis*Mathf.Sin(shakeDirection);
- if(skyBg != null)
- {
- skyBg.Update();
- }
- }
- else
- {
- offsetX = 0;
- offsetZ = 0;
- this.enabled = false;
- }
- }
- }
|