SkeletonRootMotionBase.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 UnityEngine;
  30. using System.Collections.Generic;
  31. using Spine.Unity.AnimationTools;
  32. namespace Spine.Unity {
  33. /// <summary>
  34. /// Base class for skeleton root motion components.
  35. /// </summary>
  36. abstract public class SkeletonRootMotionBase : MonoBehaviour {
  37. #region Inspector
  38. [SpineBone]
  39. [SerializeField]
  40. protected string rootMotionBoneName = "root";
  41. public bool transformPositionX = true;
  42. public bool transformPositionY = true;
  43. [Header("Optional")]
  44. public Rigidbody2D rigidBody2D;
  45. public Rigidbody rigidBody;
  46. public bool UsesRigidbody {
  47. get { return rigidBody != null || rigidBody2D != null; }
  48. }
  49. #endregion
  50. protected ISkeletonComponent skeletonComponent;
  51. protected Bone rootMotionBone;
  52. protected int rootMotionBoneIndex;
  53. protected List<Bone> topLevelBones = new List<Bone>();
  54. protected Vector2 rigidbodyDisplacement;
  55. protected virtual void Reset () {
  56. FindRigidbodyComponent();
  57. }
  58. protected virtual void Start () {
  59. skeletonComponent = GetComponent<ISkeletonComponent>();
  60. GatherTopLevelBones();
  61. SetRootMotionBone(rootMotionBoneName);
  62. var skeletonAnimation = skeletonComponent as ISkeletonAnimation;
  63. if (skeletonAnimation != null)
  64. skeletonAnimation.UpdateLocal += HandleUpdateLocal;
  65. }
  66. abstract protected Vector2 CalculateAnimationsMovementDelta ();
  67. protected virtual float AdditionalScale { get { return 1.0f; } }
  68. protected Vector2 GetTimelineMovementDelta (float startTime, float endTime,
  69. TranslateTimeline timeline, Animation animation) {
  70. Vector2 currentDelta;
  71. if (startTime > endTime) // Looped
  72. currentDelta = (timeline.Evaluate(animation.duration) - timeline.Evaluate(startTime))
  73. + (timeline.Evaluate(endTime) - timeline.Evaluate(0));
  74. else if (startTime != endTime) // Non-looped
  75. currentDelta = timeline.Evaluate(endTime) - timeline.Evaluate(startTime);
  76. else
  77. currentDelta = Vector2.zero;
  78. return currentDelta;
  79. }
  80. void GatherTopLevelBones () {
  81. topLevelBones.Clear();
  82. var skeleton = skeletonComponent.Skeleton;
  83. foreach (var bone in skeleton.Bones) {
  84. if (bone.Parent == null)
  85. topLevelBones.Add(bone);
  86. }
  87. }
  88. public void SetRootMotionBone (string name) {
  89. var skeleton = skeletonComponent.Skeleton;
  90. int index = skeleton.FindBoneIndex(name);
  91. if (index >= 0) {
  92. this.rootMotionBoneIndex = index;
  93. this.rootMotionBone = skeleton.bones.Items[index];
  94. }
  95. else {
  96. Debug.Log("Bone named \"" + name + "\" could not be found.");
  97. this.rootMotionBoneIndex = 0;
  98. this.rootMotionBone = skeleton.RootBone;
  99. }
  100. }
  101. void HandleUpdateLocal (ISkeletonAnimation animatedSkeletonComponent) {
  102. if (!this.isActiveAndEnabled)
  103. return; // Root motion is only applied when component is enabled.
  104. var movementDelta = CalculateAnimationsMovementDelta();
  105. AdjustMovementDeltaToConfiguration(ref movementDelta, animatedSkeletonComponent.Skeleton);
  106. ApplyRootMotion(movementDelta);
  107. }
  108. void AdjustMovementDeltaToConfiguration (ref Vector2 localDelta, Skeleton skeleton) {
  109. if (skeleton.ScaleX < 0) localDelta.x = -localDelta.x;
  110. if (skeleton.ScaleY < 0) localDelta.y = -localDelta.y;
  111. if (!transformPositionX) localDelta.x = 0f;
  112. if (!transformPositionY) localDelta.y = 0f;
  113. }
  114. void ApplyRootMotion (Vector2 localDelta) {
  115. localDelta *= AdditionalScale;
  116. // Apply root motion to Transform or RigidBody;
  117. if (UsesRigidbody) {
  118. rigidbodyDisplacement += (Vector2)transform.TransformVector(localDelta);
  119. // Accumulated displacement is applied on the next Physics update (FixedUpdate)
  120. }
  121. else {
  122. transform.position += transform.TransformVector(localDelta);
  123. }
  124. // Move top level bones in opposite direction of the root motion bone
  125. foreach (var topLevelBone in topLevelBones) {
  126. if (transformPositionX) topLevelBone.x -= rootMotionBone.x;
  127. if (transformPositionY) topLevelBone.y -= rootMotionBone.y;
  128. }
  129. }
  130. protected virtual void FixedUpdate () {
  131. if (!this.isActiveAndEnabled)
  132. return; // Root motion is only applied when component is enabled.
  133. if(rigidBody2D != null) {
  134. rigidBody2D.MovePosition(new Vector2(transform.position.x, transform.position.y)
  135. + rigidbodyDisplacement);
  136. }
  137. if (rigidBody != null) {
  138. rigidBody.MovePosition(transform.position
  139. + new Vector3(rigidbodyDisplacement.x, rigidbodyDisplacement.y, 0));
  140. }
  141. rigidbodyDisplacement = Vector2.zero;
  142. }
  143. protected virtual void OnDisable () {
  144. rigidbodyDisplacement = Vector2.zero;
  145. }
  146. protected void FindRigidbodyComponent () {
  147. rigidBody2D = this.GetComponent<Rigidbody2D>();
  148. if (!rigidBody2D)
  149. rigidBody = this.GetComponent<Rigidbody>();
  150. if (!rigidBody2D && !rigidBody) {
  151. rigidBody2D = this.GetComponentInParent<Rigidbody2D>();
  152. if (!rigidBody2D)
  153. rigidBody = this.GetComponentInParent<Rigidbody>();
  154. }
  155. }
  156. }
  157. }