当前位置:   article > 正文

Unity通过点击鼠标“开镜”效果(内含数值缓慢变化方法)_unity fps 开镜

unity fps 开镜

 通过点击鼠标左键达到“开镜”效果,代码如下:

  1. using DG.Tweening;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class AdjustFOV : MonoBehaviour
  6. {
  7. //相机视野缩减等级
  8. private enum GandeFOV
  9. {
  10. one,two,three,four,Null
  11. }
  12. private Camera m_camera;
  13. private float initfov;
  14. private GandeFOV gf=GandeFOV .Null;
  15. private bool useFour ;
  16. void Start()
  17. {
  18. m_camera = GetComponent<Camera>();
  19. initfov = m_camera.fieldOfView;//记录相机初始视野值
  20. }
  21. void Update()
  22. {
  23. if (Input.GetKeyDown (KeyCode.Alpha1))
  24. {
  25. gf = GandeFOV.one;
  26. }
  27. if (Input.GetKeyDown (KeyCode.Alpha2 ))
  28. {
  29. gf = GandeFOV.two;
  30. }
  31. if (Input.GetKeyDown (KeyCode.Alpha3 ))
  32. {
  33. gf = GandeFOV.three;
  34. }
  35. if (Input.GetKeyDown (KeyCode.Alpha4 ))
  36. {
  37. gf = GandeFOV.four;
  38. }
  39. //按下鼠标左键缩减相机视野
  40. if (Input .GetMouseButtonDown(0))
  41. {
  42. switch (gf)
  43. {
  44. //直接变
  45. case GandeFOV.one:
  46. m_camera.fieldOfView = 40;
  47. break;
  48. //缓慢变
  49. case GandeFOV.two:
  50. StartCoroutine("ChangeFOV");
  51. break;
  52. //平滑缓慢变
  53. case GandeFOV.three:
  54. //方法原理:在某段时间(1s)内,局部变量(currentfov)持续向目标结果(40)变化,
  55. //目标变量(m_camera.fieldOfView)持续接收局部变量的值
  56. float currentfov = m_camera.fieldOfView;
  57. DOTween.To(() => currentfov, x => currentfov = x, 40, 1)
  58. .OnUpdate(() => m_camera.fieldOfView = currentfov);
  59. break;
  60. //平滑缓慢变
  61. case GandeFOV.four:
  62. useFour = true;
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. //抬起鼠标左键恢复相机视野
  69. if (Input.GetMouseButtonUp(0))
  70. {
  71. if (gf==GandeFOV.one)
  72. {
  73. m_camera.fieldOfView = initfov;
  74. }
  75. if (gf==GandeFOV .two)
  76. {
  77. StopCoroutine("ChangeFOV");
  78. m_camera.fieldOfView = initfov;
  79. }
  80. if (gf==GandeFOV.three )
  81. {
  82. float currentfov = m_camera.fieldOfView;
  83. DOTween.To(() => currentfov, x => currentfov = x, initfov, 1)
  84. .OnUpdate(() => m_camera.fieldOfView = currentfov);
  85. }
  86. if (gf==GandeFOV.four)
  87. {
  88. useFour = false;
  89. }
  90. }
  91. if (gf == GandeFOV.four)
  92. {
  93. if (useFour)
  94. {
  95. //Mathf.Lera(起点值,终点值,比例)
  96. m_camera.fieldOfView = Mathf.Lerp(m_camera.fieldOfView, 40, 0.1f);
  97. if (Mathf.Abs(m_camera.fieldOfView - 40) < 0.1f)
  98. m_camera.fieldOfView = 40;
  99. }
  100. else
  101. {
  102. m_camera.fieldOfView = Mathf.Lerp(m_camera.fieldOfView, initfov, 0.1f);
  103. if (Mathf.Abs(m_camera.fieldOfView - initfov) < 0.1f)
  104. m_camera.fieldOfView = initfov;
  105. }
  106. }
  107. }
  108. //每等0.2秒缩减5米的相机视野,效果不流畅
  109. private IEnumerator ChangeFOV()
  110. {
  111. while (m_camera .fieldOfView >40)
  112. {
  113. m_camera.fieldOfView -= 5;
  114. yield return new WaitForSeconds(0.2f);
  115. }
  116. }
  117. }

数据缓慢变化的方法:

1、Mathf.Lera(起点值,终点值,比例)

2、 DOTween.To(() =>起点变量 , x => 起点变量 = x, 终点值, 1)
                   .OnUpdate(() => 需改变的变量 = 起点变量);

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

闽ICP备14008679号