RaggedySpineboy.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.Unity;
  30. using System.Collections;
  31. using UnityEngine;
  32. namespace Spine.Unity.Examples {
  33. public class RaggedySpineboy : MonoBehaviour {
  34. public LayerMask groundMask;
  35. public float restoreDuration = 0.5f;
  36. public Vector2 launchVelocity = new Vector2(50, 100);
  37. Spine.Unity.Examples.SkeletonRagdoll2D ragdoll;
  38. Collider2D naturalCollider;
  39. void Start () {
  40. ragdoll = GetComponent<Spine.Unity.Examples.SkeletonRagdoll2D>();
  41. naturalCollider = GetComponent<Collider2D>();
  42. }
  43. void AddRigidbody () {
  44. var rb = gameObject.AddComponent<Rigidbody2D>();
  45. rb.freezeRotation = true;
  46. naturalCollider.enabled = true;
  47. }
  48. void RemoveRigidbody () {
  49. Destroy(GetComponent<Rigidbody2D>());
  50. naturalCollider.enabled = false;
  51. }
  52. void OnMouseUp () {
  53. if (naturalCollider.enabled)
  54. Launch();
  55. }
  56. void Launch () {
  57. RemoveRigidbody();
  58. ragdoll.Apply();
  59. ragdoll.RootRigidbody.velocity = new Vector2(Random.Range(-launchVelocity.x, launchVelocity.x), launchVelocity.y);
  60. StartCoroutine(WaitUntilStopped());
  61. }
  62. IEnumerator Restore () {
  63. Vector3 estimatedPos = ragdoll.EstimatedSkeletonPosition;
  64. Vector3 rbPosition = ragdoll.RootRigidbody.position;
  65. Vector3 skeletonPoint = estimatedPos;
  66. RaycastHit2D hit = Physics2D.Raycast((Vector2)rbPosition, (Vector2)(estimatedPos - rbPosition), Vector3.Distance(estimatedPos, rbPosition), groundMask);
  67. if (hit.collider != null)
  68. skeletonPoint = hit.point;
  69. ragdoll.RootRigidbody.isKinematic = true;
  70. ragdoll.SetSkeletonPosition(skeletonPoint);
  71. yield return ragdoll.SmoothMix(0, restoreDuration);
  72. ragdoll.Remove();
  73. AddRigidbody();
  74. }
  75. IEnumerator WaitUntilStopped () {
  76. yield return new WaitForSeconds(0.5f);
  77. float t = 0;
  78. while (t < 0.5f) {
  79. t = (ragdoll.RootRigidbody.velocity.magnitude > 0.09f) ? 0 : t + Time.deltaTime;
  80. yield return null;
  81. }
  82. StartCoroutine(Restore());
  83. }
  84. }
  85. }