CVR_SawingEffect.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace ChivaXR.Tool
  6. {
  7. /// <summary>
  8. /// 锯断效果
  9. /// </summary>
  10. public class CVR_SawingEffect : Singleton<CVR_SawingEffect>
  11. {
  12. /// <summary>
  13. /// 锯断效果依附的旋转点
  14. /// </summary>
  15. public Transform m_RotationPoint;
  16. /// <summary>
  17. /// 带有刚体的目标
  18. /// </summary>
  19. public Rigidbody m_TargetRigidbody;
  20. /// <summary>
  21. /// 模型朝向
  22. /// </summary>
  23. private ModelTowards m_ModelTowards;
  24. /// <summary>
  25. /// 设置初始状态
  26. /// </summary>
  27. /// <param name="_point"></param>
  28. /// <param name="_rigidbody"></param>
  29. /// <param name="_state"></param>
  30. public void SetStartStateData(Transform _point, Rigidbody _rigidbody, ModelTowards _state)
  31. {
  32. this.m_RotationPoint = _point;
  33. this.m_TargetRigidbody = _rigidbody;
  34. this.m_ModelTowards = _state;
  35. CVR_SawingSystem.SetSawingOpState(this, 0, 0);
  36. }
  37. /// <summary>
  38. /// 每帧执行
  39. /// </summary>
  40. public void SimulationUpdate(float _currentMoveDis, float _value, Action _action)
  41. {
  42. CVR_SawingSystem.SawingOpSimulation(this, _currentMoveDis, _value, m_ModelTowards, _action);
  43. }
  44. }
  45. /// <summary>
  46. /// 锯切系统
  47. /// </summary>
  48. internal class CVR_SawingSystem
  49. {
  50. /// <summary>
  51. /// 锯切操作控制器
  52. /// </summary>
  53. /// <param name="_data"></param>
  54. /// <param name="_currentMoveDis"></param>
  55. /// <param name="_value"></param>
  56. /// <param name="_action"></param>
  57. public static void SawingOpSimulation(CVR_SawingEffect _data, float _currentMoveDis, float _value, ModelTowards _modelTowards, Action _action)
  58. {
  59. if (_value < 0.0f)
  60. {
  61. return;
  62. }
  63. if (_value < 0.6f)
  64. {
  65. _data.m_RotationPoint.localEulerAngles = Vector3.Lerp(Vector3.zero, new Vector3(0, 0, SetDistance(_modelTowards, 5)), (_currentMoveDis / 0.6f) * _value);
  66. }
  67. else if (_value < 0.8f)
  68. {
  69. _data.m_RotationPoint.localEulerAngles = Vector3.Lerp(new Vector3(0, 0, SetDistance(_modelTowards, 5)), new Vector3(0, 0, SetDistance(_modelTowards, 10)), (_currentMoveDis / 0.2f) * _value - 3);
  70. }
  71. else if (_value < 1f)
  72. {
  73. _data.m_RotationPoint.localEulerAngles = Vector3.Lerp(new Vector3(0, 0, SetDistance(_modelTowards, 10)), new Vector3(0, 0, SetDistance(_modelTowards, 75)), (_currentMoveDis / 0.2f) * _value - 4);
  74. }
  75. else
  76. {
  77. if (_data.m_TargetRigidbody != null)
  78. {
  79. _data.m_TargetRigidbody.useGravity = true;
  80. _data.m_TargetRigidbody.isKinematic = false;
  81. }
  82. if (_data.m_TargetRigidbody.GetComponent<Collider>() != null)
  83. {
  84. _data.m_TargetRigidbody.GetComponent<Collider>().isTrigger = false;
  85. }
  86. _action?.Invoke();
  87. }
  88. }
  89. /// <summary>
  90. /// 锯断电缆状态变化
  91. /// </summary>
  92. /// <param name="_data"></param>
  93. /// <param name="_value"></param>
  94. public static void SetSawingOpState(CVR_SawingEffect _data, float _currentMoveDis, float _value)
  95. {
  96. if (_value < 0.0f)
  97. {
  98. return;
  99. }
  100. if (_value < 0.6f)
  101. {
  102. _data.m_RotationPoint.localEulerAngles = Vector3.Lerp(Vector3.zero, new Vector3(0, 0, 5), (_currentMoveDis / 0.6f) * _value);
  103. }
  104. else if (_value < 0.8f)
  105. {
  106. _data.m_RotationPoint.localEulerAngles = Vector3.Lerp(new Vector3(0, 0, 5), new Vector3(0, 0, 10), (_currentMoveDis / 0.2f) * _value - 3);
  107. }
  108. else if (_value <= 1f)
  109. {
  110. _data.m_RotationPoint.localEulerAngles = Vector3.Lerp(new Vector3(0, 0, 10), new Vector3(0, 0, 75), (_currentMoveDis / 0.2f) * _value - 4);
  111. }
  112. }
  113. /// <summary>
  114. /// 设置距离值
  115. /// </summary>
  116. /// <param name="_state"></param>
  117. /// <param name="_dis"></param>
  118. /// <returns></returns>
  119. private static float SetDistance(ModelTowards _state, float _dis)
  120. {
  121. return _state == ModelTowards.Positive ? Mathf.Abs(_dis) : -Mathf.Abs(_dis);
  122. }
  123. }
  124. /// <summary>
  125. /// 模型朝向
  126. /// </summary>
  127. public enum ModelTowards
  128. {
  129. /// <summary>
  130. /// 正
  131. /// </summary>
  132. Positive,
  133. /// <summary>
  134. /// 负
  135. /// </summary>
  136. Negative,
  137. }
  138. }