SpineboyFootplanter.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. using Spine;
  30. using Spine.Unity;
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. using UnityEngine;
  34. namespace Spine.Unity.Examples {
  35. public class SpineboyFootplanter : MonoBehaviour {
  36. public float timeScale = 0.5f;
  37. [SpineBone] public string nearBoneName, farBoneName;
  38. [Header("Settings")]
  39. public Vector2 footSize;
  40. public float footRayRaise = 2f;
  41. public float comfyDistance = 1f;
  42. public float centerOfGravityXOffset = -0.25f;
  43. public float feetTooFarApartThreshold = 3f;
  44. public float offBalanceThreshold = 1.4f;
  45. public float minimumSpaceBetweenFeet = 0.5f;
  46. public float maxNewStepDisplacement = 2f;
  47. public float shuffleDistance = 1f;
  48. public float baseLerpSpeed = 3.5f;
  49. public FootMovement forward, backward;
  50. [Header("Debug")]
  51. [SerializeField] float balance;
  52. [SerializeField] float distanceBetweenFeet;
  53. [SerializeField] protected Foot nearFoot, farFoot;
  54. Skeleton skeleton;
  55. Bone nearFootBone, farFootBone;
  56. [System.Serializable]
  57. public class FootMovement {
  58. public AnimationCurve xMoveCurve;
  59. public AnimationCurve raiseCurve;
  60. public float maxRaise;
  61. public float minDistanceCompensate;
  62. public float maxDistanceCompensate;
  63. }
  64. [System.Serializable]
  65. public class Foot {
  66. public Vector2 worldPos;
  67. public float displacementFromCenter;
  68. public float distanceFromCenter;
  69. [Space]
  70. public float lerp;
  71. public Vector2 worldPosPrev;
  72. public Vector2 worldPosNext;
  73. public bool IsStepInProgress { get { return lerp < 1f; } }
  74. public bool IsPrettyMuchDoneStepping { get { return lerp > 0.7f; } }
  75. public void UpdateDistance (float centerOfGravityX) {
  76. displacementFromCenter = worldPos.x - centerOfGravityX;
  77. distanceFromCenter = Mathf.Abs(displacementFromCenter);
  78. }
  79. public void StartNewStep (float newDistance, float centerOfGravityX, float tentativeY, float footRayRaise, RaycastHit2D[] hits, Vector2 footSize) {
  80. lerp = 0f;
  81. worldPosPrev = worldPos;
  82. float newX = centerOfGravityX - newDistance;
  83. Vector2 origin = new Vector2(newX, tentativeY + footRayRaise);
  84. //int hitCount = Physics2D.BoxCastNonAlloc(origin, footSize, 0f, Vector2.down, hits);
  85. int hitCount = Physics2D.BoxCast(origin, footSize, 0f, Vector2.down, new ContactFilter2D { useTriggers = false }, hits);
  86. worldPosNext = hitCount > 0 ? hits[0].point : new Vector2(newX, tentativeY);
  87. }
  88. public void UpdateStepProgress (float deltaTime, float stepSpeed, float shuffleDistance, FootMovement forwardMovement, FootMovement backwardMovement) {
  89. if (!this.IsStepInProgress)
  90. return;
  91. lerp += deltaTime * stepSpeed;
  92. float strideSignedSize = worldPosNext.x - worldPosPrev.x;
  93. float strideSign = Mathf.Sign(strideSignedSize);
  94. float strideSize = (Mathf.Abs(strideSignedSize));
  95. var movement = strideSign > 0 ? forwardMovement : backwardMovement;
  96. worldPos.x = Mathf.Lerp(worldPosPrev.x, worldPosNext.x, movement.xMoveCurve.Evaluate(lerp));
  97. float groundLevel = Mathf.Lerp(worldPosPrev.y, worldPosNext.y, lerp);
  98. if (strideSize > shuffleDistance) {
  99. float strideSizeFootRaise = Mathf.Clamp((strideSize * 0.5f), 1f, 2f);
  100. worldPos.y = groundLevel + (movement.raiseCurve.Evaluate(lerp) * movement.maxRaise * strideSizeFootRaise);
  101. } else {
  102. lerp += Time.deltaTime;
  103. worldPos.y = groundLevel;
  104. }
  105. if (lerp > 1f)
  106. lerp = 1f;
  107. }
  108. public static float GetNewDisplacement (float otherLegDisplacementFromCenter, float comfyDistance, float minimumFootDistanceX, float maxNewStepDisplacement, FootMovement forwardMovement, FootMovement backwardMovement) {
  109. var movement = Mathf.Sign(otherLegDisplacementFromCenter) < 0 ? forwardMovement : backwardMovement;
  110. float randomCompensate = Random.Range(movement.minDistanceCompensate, movement.maxDistanceCompensate);
  111. float newDisplacement = (otherLegDisplacementFromCenter * randomCompensate);
  112. if (Mathf.Abs(newDisplacement) > maxNewStepDisplacement || Mathf.Abs(otherLegDisplacementFromCenter) < minimumFootDistanceX)
  113. newDisplacement = comfyDistance * Mathf.Sign(newDisplacement) * randomCompensate;
  114. return newDisplacement;
  115. }
  116. }
  117. public float Balance { get { return balance; } }
  118. void Start () {
  119. Time.timeScale = timeScale;
  120. var tpos = transform.position;
  121. // Default starting positions.
  122. nearFoot.worldPos = tpos;
  123. nearFoot.worldPos.x -= comfyDistance;
  124. nearFoot.worldPosPrev = nearFoot.worldPosNext = nearFoot.worldPos;
  125. farFoot.worldPos = tpos;
  126. farFoot.worldPos.x += comfyDistance;
  127. farFoot.worldPosPrev = farFoot.worldPosNext = farFoot.worldPos;
  128. var skeletonAnimation = GetComponent<SkeletonAnimation>();
  129. skeleton = skeletonAnimation.Skeleton;
  130. skeletonAnimation.UpdateLocal += UpdateLocal;
  131. nearFootBone = skeleton.FindBone(nearBoneName);
  132. farFootBone = skeleton.FindBone(farBoneName);
  133. nearFoot.lerp = 1f;
  134. farFoot.lerp = 1f;
  135. }
  136. RaycastHit2D[] hits = new RaycastHit2D[1];
  137. private void UpdateLocal (ISkeletonAnimation animated) {
  138. Transform thisTransform = transform;
  139. Vector2 thisTransformPosition = thisTransform.position;
  140. float centerOfGravityX = thisTransformPosition.x + centerOfGravityXOffset;
  141. nearFoot.UpdateDistance(centerOfGravityX);
  142. farFoot.UpdateDistance(centerOfGravityX);
  143. balance = nearFoot.displacementFromCenter + farFoot.displacementFromCenter;
  144. distanceBetweenFeet = Mathf.Abs(nearFoot.worldPos.x - farFoot.worldPos.x);
  145. // Detect time to make a new step
  146. bool isTooOffBalance = Mathf.Abs(balance) > offBalanceThreshold;
  147. bool isFeetTooFarApart = distanceBetweenFeet > feetTooFarApartThreshold;
  148. bool timeForNewStep = isFeetTooFarApart || isTooOffBalance;
  149. if (timeForNewStep) {
  150. // Choose which foot to use for next step.
  151. Foot stepFoot, otherFoot;
  152. bool stepLegIsNearLeg = nearFoot.distanceFromCenter > farFoot.distanceFromCenter;
  153. if (stepLegIsNearLeg) {
  154. stepFoot = nearFoot;
  155. otherFoot = farFoot;
  156. } else {
  157. stepFoot = farFoot;
  158. otherFoot = nearFoot;
  159. }
  160. // Start a new step.
  161. if (!stepFoot.IsStepInProgress && otherFoot.IsPrettyMuchDoneStepping) {
  162. float newDisplacement = Foot.GetNewDisplacement(otherFoot.displacementFromCenter, comfyDistance, minimumSpaceBetweenFeet, maxNewStepDisplacement, forward, backward);
  163. stepFoot.StartNewStep(newDisplacement, centerOfGravityX, thisTransformPosition.y, footRayRaise, hits, footSize);
  164. }
  165. }
  166. float deltaTime = Time.deltaTime;
  167. float stepSpeed = baseLerpSpeed;
  168. stepSpeed += (Mathf.Abs(balance) - 0.6f) * 2.5f;
  169. // Animate steps that are in progress.
  170. nearFoot.UpdateStepProgress(deltaTime, stepSpeed, shuffleDistance, forward, backward);
  171. farFoot.UpdateStepProgress(deltaTime, stepSpeed, shuffleDistance, forward, backward);
  172. nearFootBone.SetLocalPosition(thisTransform.InverseTransformPoint(nearFoot.worldPos));
  173. farFootBone.SetLocalPosition(thisTransform.InverseTransformPoint(farFoot.worldPos));
  174. }
  175. void OnDrawGizmos () {
  176. if (Application.isPlaying) {
  177. const float Radius = 0.15f;
  178. Gizmos.color = Color.green;
  179. Gizmos.DrawSphere(nearFoot.worldPos, Radius);
  180. Gizmos.DrawWireSphere(nearFoot.worldPosNext, Radius);
  181. Gizmos.color = Color.magenta;
  182. Gizmos.DrawSphere(farFoot.worldPos, Radius);
  183. Gizmos.DrawWireSphere(farFoot.worldPosNext, Radius);
  184. }
  185. }
  186. }
  187. }