SkeletonUtilityGroundConstraint.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using UnityEngine;
  2. using System.Collections;
  3. [RequireComponent(typeof(SkeletonUtilityBone)), ExecuteInEditMode]
  4. public class SkeletonUtilityGroundConstraint : SkeletonUtilityConstraint
  5. {
  6. #if UNITY_4_3
  7. public LayerMask groundMask;
  8. public bool use2D = false;
  9. public bool useRadius = false;
  10. public float castRadius = 0.1f;
  11. public float castDistance = 5f;
  12. public float castOffset = 0;
  13. public float groundOffset = 0;
  14. public float adjustSpeed = 5;
  15. #else
  16. [Tooltip("LayerMask for what objects to raycast against")]
  17. public LayerMask groundMask;
  18. [Tooltip("The 2D")]
  19. public bool use2D = false;
  20. [Tooltip("Uses SphereCast for 3D mode and CircleCast for 2D mode")]
  21. public bool useRadius = false;
  22. [Tooltip("The Radius")]
  23. public float castRadius = 0.1f;
  24. [Tooltip("How high above the target bone to begin casting from")]
  25. public float castDistance = 5f;
  26. [Tooltip("X-Axis adjustment")]
  27. public float castOffset = 0;
  28. [Tooltip("Y-Axis adjustment")]
  29. public float groundOffset = 0;
  30. [Tooltip("How fast the target IK position adjusts to the ground. Use smaller values to prevent snapping")]
  31. public float adjustSpeed = 5;
  32. #endif
  33. Vector3 rayOrigin;
  34. Vector3 rayDir = new Vector3(0, -1, 0);
  35. float hitY;
  36. float lastHitY;
  37. protected override void OnEnable()
  38. {
  39. base.OnEnable();
  40. }
  41. protected override void OnDisable()
  42. {
  43. base.OnDisable();
  44. }
  45. public override void DoUpdate()
  46. {
  47. rayOrigin = transform.position + new Vector3(castOffset, castDistance, 0);
  48. hitY = float.MinValue;
  49. if (use2D)
  50. {
  51. RaycastHit2D hit;
  52. if (useRadius)
  53. {
  54. #if UNITY_4_3
  55. //NOTE: Unity 4.3.x does not have CircleCast
  56. hit = Physics2D.Raycast(rayOrigin , rayDir, castDistance + groundOffset, groundMask);
  57. #else
  58. hit = Physics2D.CircleCast(rayOrigin, castRadius, rayDir, castDistance + groundOffset, groundMask);
  59. #endif
  60. }
  61. else
  62. {
  63. hit = Physics2D.Raycast(rayOrigin, rayDir, castDistance + groundOffset, groundMask);
  64. }
  65. if (hit.collider != null)
  66. {
  67. hitY = hit.point.y + groundOffset;
  68. if (Application.isPlaying)
  69. {
  70. hitY = Mathf.MoveTowards(lastHitY, hitY, adjustSpeed * Time.deltaTime);
  71. }
  72. }
  73. else
  74. {
  75. if (Application.isPlaying)
  76. hitY = Mathf.MoveTowards(lastHitY, transform.position.y, adjustSpeed * Time.deltaTime);
  77. }
  78. }
  79. else
  80. {
  81. RaycastHit hit;
  82. bool validHit = false;
  83. if (useRadius)
  84. {
  85. validHit = Physics.SphereCast(rayOrigin, castRadius, rayDir, out hit, castDistance + groundOffset, groundMask);
  86. }
  87. else
  88. {
  89. validHit = Physics.Raycast(rayOrigin, rayDir, out hit, castDistance + groundOffset, groundMask);
  90. }
  91. if (validHit)
  92. {
  93. hitY = hit.point.y + groundOffset;
  94. if (Application.isPlaying)
  95. {
  96. hitY = Mathf.MoveTowards(lastHitY, hitY, adjustSpeed * Time.deltaTime);
  97. }
  98. }
  99. else
  100. {
  101. if (Application.isPlaying)
  102. hitY = Mathf.MoveTowards(lastHitY, transform.position.y, adjustSpeed * Time.deltaTime);
  103. }
  104. }
  105. Vector3 v = transform.position;
  106. v.y = Mathf.Clamp(v.y, Mathf.Min(lastHitY, hitY), float.MaxValue);
  107. transform.position = v;
  108. utilBone.bone.X = transform.localPosition.x;
  109. utilBone.bone.Y = transform.localPosition.y;
  110. lastHitY = hitY;
  111. }
  112. void OnDrawGizmos()
  113. {
  114. Vector3 hitEnd = rayOrigin + (rayDir * Mathf.Min(castDistance, rayOrigin.y - hitY));
  115. Vector3 clearEnd = rayOrigin + (rayDir * castDistance);
  116. Gizmos.DrawLine(rayOrigin, hitEnd);
  117. if (useRadius)
  118. {
  119. Gizmos.DrawLine(new Vector3(hitEnd.x - castRadius, hitEnd.y - groundOffset, hitEnd.z), new Vector3(hitEnd.x + castRadius, hitEnd.y - groundOffset, hitEnd.z));
  120. Gizmos.DrawLine(new Vector3(clearEnd.x - castRadius, clearEnd.y, clearEnd.z), new Vector3(clearEnd.x + castRadius, clearEnd.y, clearEnd.z));
  121. }
  122. Gizmos.color = Color.red;
  123. Gizmos.DrawLine(hitEnd, clearEnd);
  124. }
  125. }