SpringBone.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. //SpringBone.cs for unity-chan!
  3. //
  4. //Original Script is here:
  5. //ricopin / SpringBone.cs
  6. //Rocket Jump : http://rocketjump.skr.jp/unity3d/109/
  7. //https://twitter.com/ricopin416
  8. //
  9. //Revised by N.Kobayashi 2014/06/20
  10. //
  11. using UnityEngine;
  12. using System.Collections;
  13. namespace UnityChan
  14. {
  15. public class SpringBone : MonoBehaviour
  16. {
  17. //次のボーン
  18. public Transform child;
  19. //ボーンの向き
  20. public Vector3 boneAxis = new Vector3 (-1.0f, 0.0f, 0.0f);
  21. public float radius = 0.05f;
  22. //各SpringBoneに設定されているstiffnessForceとdragForceを使用するか?
  23. public bool isUseEachBoneForceSettings = false;
  24. //バネが戻る力
  25. public float stiffnessForce = 0.01f;
  26. //力の減衰力
  27. public float dragForce = 0.4f;
  28. public Vector3 springForce = new Vector3 (0.0f, -0.0001f, 0.0f);
  29. public SpringCollider[] colliders;
  30. public bool debug = true;
  31. //Kobayashi:Thredshold Starting to activate activeRatio
  32. public float threshold = 0.01f;
  33. private float springLength;
  34. private Quaternion localRotation;
  35. private Transform trs;
  36. private Vector3 currTipPos;
  37. private Vector3 prevTipPos;
  38. //Kobayashi
  39. private Transform org;
  40. //Kobayashi:Reference for "SpringManager" component with unitychan
  41. private SpringManager managerRef;
  42. private void Awake ()
  43. {
  44. trs = transform;
  45. localRotation = transform.localRotation;
  46. //Kobayashi:Reference for "SpringManager" component with unitychan
  47. // GameObject.Find("unitychan_dynamic").GetComponent<SpringManager>();
  48. managerRef = GetParentSpringManager (transform);
  49. }
  50. private SpringManager GetParentSpringManager (Transform t)
  51. {
  52. var springManager = t.GetComponent<SpringManager> ();
  53. if (springManager != null)
  54. return springManager;
  55. if (t.parent != null) {
  56. return GetParentSpringManager (t.parent);
  57. }
  58. return null;
  59. }
  60. private void Start ()
  61. {
  62. springLength = Vector3.Distance (trs.position, child.position);
  63. currTipPos = child.position;
  64. prevTipPos = child.position;
  65. }
  66. public void UpdateSpring ()
  67. {
  68. //Kobayashi
  69. org = trs;
  70. //回転をリセット
  71. trs.localRotation = Quaternion.identity * localRotation;
  72. float sqrDt = Time.deltaTime * Time.deltaTime;
  73. //stiffness
  74. Vector3 force = trs.rotation * (boneAxis * stiffnessForce) / sqrDt;
  75. //drag
  76. force += (prevTipPos - currTipPos) * dragForce / sqrDt;
  77. force += springForce / sqrDt;
  78. //前フレームと値が同じにならないように
  79. Vector3 temp = currTipPos;
  80. //verlet
  81. currTipPos = (currTipPos - prevTipPos) + currTipPos + (force * sqrDt);
  82. //長さを元に戻す
  83. currTipPos = ((currTipPos - trs.position).normalized * springLength) + trs.position;
  84. //衝突判定
  85. for (int i = 0; i < colliders.Length; i++) {
  86. if (Vector3.Distance (currTipPos, colliders [i].transform.position) <= (radius + colliders [i].radius)) {
  87. Vector3 normal = (currTipPos - colliders [i].transform.position).normalized;
  88. currTipPos = colliders [i].transform.position + (normal * (radius + colliders [i].radius));
  89. currTipPos = ((currTipPos - trs.position).normalized * springLength) + trs.position;
  90. }
  91. }
  92. prevTipPos = temp;
  93. //回転を適用;
  94. Vector3 aimVector = trs.TransformDirection (boneAxis);
  95. Quaternion aimRotation = Quaternion.FromToRotation (aimVector, currTipPos - trs.position);
  96. //original
  97. //trs.rotation = aimRotation * trs.rotation;
  98. //Kobayahsi:Lerp with mixWeight
  99. Quaternion secondaryRotation = aimRotation * trs.rotation;
  100. trs.rotation = Quaternion.Lerp (org.rotation, secondaryRotation, managerRef.dynamicRatio);
  101. }
  102. private void OnDrawGizmos ()
  103. {
  104. if (debug) {
  105. Gizmos.color = Color.yellow;
  106. Gizmos.DrawWireSphere (currTipPos, radius);
  107. }
  108. }
  109. }
  110. }