CameraShoke.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using UnityEngine;
  2. using System.Collections;
  3. public class CameraShoke : MonoBehaviour {
  4. public Transform camTransform; // 抖动目标的transform
  5. public float n_shake = 0f; //持续抖动的时长
  6. public float n_shakeAmount = 0.15f; //振幅越大抖动越厉害
  7. public float n_decreaseFactor = 1f; //震动速度
  8. private Vector3 m_v3_randomOffest = new Vector3(-0.1f,0,0.1f); //左右偏移修正
  9. private Vector3 m_v3_weight = Vector3.one;// 震动方向权重
  10. public void Init(GameObject l_go_camera )
  11. {
  12. camTransform = l_go_camera.transform;
  13. }
  14. /// <summary>
  15. /// 触发震动
  16. /// </summary>
  17. /// <param name="时长"></param>
  18. /// <param name="振幅"></param>
  19. /// <param name="震速"></param>
  20. /// <param name="偏移"></param>
  21. public void Shoke(float l_n_shokeTime, float l_n_amount, float l_n_decreaseFactor, Vector3 l_v3_randomOffest, Vector3 l_v3_weight)
  22. {
  23. n_shake = l_n_shokeTime;
  24. n_shakeAmount = l_n_amount;
  25. n_decreaseFactor = l_n_decreaseFactor;
  26. m_v3_randomOffest = l_v3_randomOffest;
  27. m_v3_weight = l_v3_weight;
  28. }
  29. public void UpdateShoke()
  30. {
  31. if (n_shake > 0)
  32. {
  33. Vector3 l_v3_randomValue = Random.insideUnitSphere * n_shakeAmount + m_v3_randomOffest;
  34. float weight = m_v3_weight.x + m_v3_weight.y + m_v3_weight.z;
  35. if (weight == 0)
  36. {
  37. return;
  38. }
  39. l_v3_randomValue.x *= (m_v3_weight.x / weight);
  40. l_v3_randomValue.y *= (m_v3_weight.y / weight);
  41. l_v3_randomValue.z *= (m_v3_weight.z / weight);
  42. //l_v3_randomValue.y = 0; //禁止y轴方向的震动,
  43. camTransform.localPosition = l_v3_randomValue;
  44. n_shake -= Time.deltaTime * n_decreaseFactor;
  45. }
  46. else
  47. {
  48. camTransform.localPosition = Vector3.zero;
  49. n_shake = 0;
  50. }
  51. }
  52. }