CameraShakingBehaviour.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using UnityEngine;
  2. using UnityEngine.Playables;
  3. using Utility;
  4. namespace CombatLibrary.CombatLibrary.CombatCore.CustomizeTime.CameraShakingAsset
  5. {
  6. public class CameraShakingBehaviour : PlayableBehaviour
  7. {
  8. public Camera Camera;
  9. public ShakeDirection direction;
  10. public int type;
  11. public float totalTime;
  12. public int count;
  13. public float qiangDu;
  14. public AnimationCurve AnimationCurve;
  15. private float _effectTime;
  16. private Vector3 _startPos;
  17. private bool _inEffect;
  18. private float cosAdd;
  19. private float currCosTime;
  20. public override void OnBehaviourPlay(Playable playable, FrameData info)
  21. {
  22. if (Camera == null)
  23. {
  24. return;
  25. }
  26. _startPos = Camera.transform.position;
  27. _effectTime = 0;
  28. _inEffect = true;
  29. cosAdd = 360F*count / totalTime;
  30. currCosTime = 0;
  31. }
  32. public override void ProcessFrame(Playable playable, FrameData info, object playerData)
  33. {
  34. if (Camera == null)
  35. {
  36. return;
  37. }
  38. ShakingPos(info.deltaTime);
  39. // Debug.Log("ProcessFrame" + Mathf.Cos(271 * Mathf.Deg2Rad));
  40. }
  41. private void ShakingPos(float time)
  42. {
  43. if (!_inEffect)
  44. {
  45. return;
  46. }
  47. _effectTime += time;
  48. currCosTime += time * cosAdd;
  49. float value = Mathf.Cos((90 + currCosTime) * Mathf.Deg2Rad);
  50. // Debug.Log(value);
  51. float _strength = AnimationCurve.Evaluate(_effectTime / totalTime);
  52. switch (direction)
  53. {
  54. case ShakeDirection.LeftRightUpDown:
  55. Camera.transform.position = _startPos + new Vector3(1,1,0)* value* _strength*qiangDu;
  56. break;
  57. case ShakeDirection.LeftRight:
  58. Camera.transform.position = _startPos + new Vector3(1,0,0)* value* _strength*qiangDu;
  59. break;
  60. case ShakeDirection.UpDown:
  61. Camera.transform.position = _startPos + new Vector3(0,1,0)* value* _strength*qiangDu;
  62. break;
  63. case ShakeDirection.FrontBack:
  64. Camera.transform.position = _startPos + new Vector3(0,0,1)* value* _strength*qiangDu;
  65. break;
  66. }
  67. if (_effectTime > totalTime)
  68. {
  69. _inEffect = false;
  70. Camera.transform.position = _startPos;
  71. }
  72. }
  73. }
  74. }