SkeletonUtilityGroundConstraint.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #if UNITY_2018_3 || UNITY_2019 || UNITY_2018_3_OR_NEWER
  30. #define NEW_PREFAB_SYSTEM
  31. #endif
  32. using UnityEngine;
  33. namespace Spine.Unity.Examples {
  34. #if NEW_PREFAB_SYSTEM
  35. [ExecuteAlways]
  36. #else
  37. [ExecuteInEditMode]
  38. #endif
  39. [RequireComponent(typeof(SkeletonUtilityBone))]
  40. public class SkeletonUtilityGroundConstraint : SkeletonUtilityConstraint {
  41. [Tooltip("LayerMask for what objects to raycast against")]
  42. public LayerMask groundMask;
  43. [Tooltip("Use 2D")]
  44. public bool use2D = false;
  45. [Tooltip("Uses SphereCast for 3D mode and CircleCast for 2D mode")]
  46. public bool useRadius = false;
  47. [Tooltip("The Radius")]
  48. public float castRadius = 0.1f;
  49. [Tooltip("How high above the target bone to begin casting from")]
  50. public float castDistance = 5f;
  51. [Tooltip("X-Axis adjustment")]
  52. public float castOffset = 0;
  53. [Tooltip("Y-Axis adjustment")]
  54. public float groundOffset = 0;
  55. [Tooltip("How fast the target IK position adjusts to the ground. Use smaller values to prevent snapping")]
  56. public float adjustSpeed = 5;
  57. Vector3 rayOrigin;
  58. Vector3 rayDir = new Vector3(0, -1, 0);
  59. float hitY;
  60. float lastHitY;
  61. protected override void OnEnable () {
  62. base.OnEnable();
  63. lastHitY = transform.position.y;
  64. }
  65. public override void DoUpdate () {
  66. rayOrigin = transform.position + new Vector3(castOffset, castDistance, 0);
  67. float positionScale = hierarchy.PositionScale;
  68. float adjustDistanceThisFrame = adjustSpeed * positionScale * Time.deltaTime;
  69. hitY = float.MinValue;
  70. if (use2D) {
  71. RaycastHit2D hit;
  72. if (useRadius)
  73. hit = Physics2D.CircleCast(rayOrigin, castRadius, rayDir, castDistance + groundOffset, groundMask);
  74. else
  75. hit = Physics2D.Raycast(rayOrigin, rayDir, castDistance + groundOffset, groundMask);
  76. if (hit.collider != null) {
  77. hitY = hit.point.y + groundOffset;
  78. if (Application.isPlaying)
  79. hitY = Mathf.MoveTowards(lastHitY, hitY, adjustDistanceThisFrame);
  80. } else {
  81. if (Application.isPlaying)
  82. hitY = Mathf.MoveTowards(lastHitY, transform.position.y, adjustDistanceThisFrame);
  83. }
  84. } else {
  85. RaycastHit hit;
  86. bool validHit = false;
  87. if (useRadius)
  88. validHit = Physics.SphereCast(rayOrigin, castRadius, rayDir, out hit, castDistance + groundOffset, groundMask);
  89. else
  90. validHit = Physics.Raycast(rayOrigin, rayDir, out hit, castDistance + groundOffset, groundMask);
  91. if (validHit) {
  92. hitY = hit.point.y + groundOffset;
  93. if (Application.isPlaying)
  94. hitY = Mathf.MoveTowards(lastHitY, hitY, adjustDistanceThisFrame);
  95. } else {
  96. if (Application.isPlaying)
  97. hitY = Mathf.MoveTowards(lastHitY, transform.position.y, adjustDistanceThisFrame);
  98. }
  99. }
  100. Vector3 v = transform.position;
  101. v.y = Mathf.Clamp(v.y, Mathf.Min(lastHitY, hitY), float.MaxValue);
  102. transform.position = v;
  103. bone.bone.X = transform.localPosition.x / hierarchy.PositionScale;
  104. bone.bone.Y = transform.localPosition.y / hierarchy.PositionScale;
  105. lastHitY = hitY;
  106. }
  107. void OnDrawGizmos () {
  108. Vector3 hitEnd = rayOrigin + (rayDir * Mathf.Min(castDistance, rayOrigin.y - hitY));
  109. Vector3 clearEnd = rayOrigin + (rayDir * castDistance);
  110. Gizmos.DrawLine(rayOrigin, hitEnd);
  111. if (useRadius) {
  112. Gizmos.DrawLine(new Vector3(hitEnd.x - castRadius, hitEnd.y - groundOffset, hitEnd.z), new Vector3(hitEnd.x + castRadius, hitEnd.y - groundOffset, hitEnd.z));
  113. Gizmos.DrawLine(new Vector3(clearEnd.x - castRadius, clearEnd.y, clearEnd.z), new Vector3(clearEnd.x + castRadius, clearEnd.y, clearEnd.z));
  114. }
  115. Gizmos.color = Color.red;
  116. Gizmos.DrawLine(hitEnd, clearEnd);
  117. }
  118. }
  119. }