当前位置:   article > 正文

Unity实现震屏效果(屏幕抖动)_unity震屏插件shake camera怎么用

unity震屏插件shake camera怎么用

     通过设置相机的Viewport Rect 来实现,将下面的脚本挂在相机上,设置enabled=true就可实现屏幕震动效果。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ShakeCamera : MonoBehaviour {
  5. public float shakeLevel = 3f;// 震动幅度
  6. public float setShakeTime = 0.5f; // 震动时间
  7. public float shakeFps = 45f; // 震动的FPS
  8. private bool isshakeCamera = false;// 震动标志
  9. private float fps;
  10. private float shakeTime = 0.0f;
  11. private float frameTime = 0.0f;
  12. private float shakeDelta = 0.005f;
  13. private Camera selfCamera;
  14. void OnEnable()
  15. {
  16. isshakeCamera = true;
  17. selfCamera = gameObject.GetComponent<Camera>();
  18. shakeTime = setShakeTime;
  19. fps = shakeFps;
  20. frameTime = 0.03f;
  21. shakeDelta = 0.005f;
  22. }
  23. void OnDisable()
  24. {
  25. selfCamera.rect = new Rect(0.0f, 0.0f, 1.0f, 1.0f);
  26. isshakeCamera = false;
  27. }
  28. // Update is called once per frame
  29. void Update()
  30. {
  31. if (isshakeCamera)
  32. {
  33. if (shakeTime > 0)
  34. {
  35. shakeTime -= Time.deltaTime;
  36. if (shakeTime <= 0)
  37. {
  38. enabled = false;
  39. }
  40. else
  41. {
  42. frameTime += Time.deltaTime;
  43. if (frameTime > 1.0 / fps)
  44. {
  45. frameTime = 0;
  46. selfCamera.rect = new Rect(shakeDelta * (-1.0f + shakeLevel * Random.value), shakeDelta * (-1.0f + shakeLevel * Random.value), 1.0f, 1.0f);
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/125398
推荐阅读
相关标签
  

闽ICP备14008679号