RaggedySpineboy.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. using UnityEngine;
  31. using System.Collections;
  32. using Spine.Unity;
  33. namespace Spine.Unity.Examples {
  34. public class RaggedySpineboy : MonoBehaviour {
  35. public LayerMask groundMask;
  36. public float restoreDuration = 0.5f;
  37. public Vector2 launchVelocity = new Vector2(50,100);
  38. Spine.Unity.Modules.SkeletonRagdoll2D ragdoll;
  39. Collider2D naturalCollider;
  40. void Start () {
  41. ragdoll = GetComponent<Spine.Unity.Modules.SkeletonRagdoll2D>();
  42. naturalCollider = GetComponent<Collider2D>();
  43. }
  44. void AddRigidbody () {
  45. var rb = gameObject.AddComponent<Rigidbody2D>();
  46. rb.freezeRotation = true;
  47. naturalCollider.enabled = true;
  48. }
  49. void RemoveRigidbody () {
  50. Destroy(GetComponent<Rigidbody2D>());
  51. naturalCollider.enabled = false;
  52. }
  53. void OnMouseUp () {
  54. if (naturalCollider.enabled)
  55. Launch();
  56. }
  57. void Launch () {
  58. RemoveRigidbody();
  59. ragdoll.Apply();
  60. ragdoll.RootRigidbody.velocity = new Vector2(Random.Range(-launchVelocity.x, launchVelocity.x), launchVelocity.y);
  61. StartCoroutine(WaitUntilStopped());
  62. }
  63. IEnumerator Restore () {
  64. Vector3 estimatedPos = ragdoll.EstimatedSkeletonPosition;
  65. Vector3 rbPosition = ragdoll.RootRigidbody.position;
  66. Vector3 skeletonPoint = estimatedPos;
  67. RaycastHit2D hit = Physics2D.Raycast((Vector2)rbPosition, (Vector2)(estimatedPos - rbPosition), Vector3.Distance(estimatedPos, rbPosition), groundMask);
  68. if (hit.collider != null)
  69. skeletonPoint = hit.point;
  70. ragdoll.RootRigidbody.isKinematic = true;
  71. ragdoll.SetSkeletonPosition(skeletonPoint);
  72. yield return ragdoll.SmoothMix(0, restoreDuration);
  73. ragdoll.Remove();
  74. AddRigidbody();
  75. }
  76. IEnumerator WaitUntilStopped () {
  77. yield return new WaitForSeconds(0.5f);
  78. float t = 0;
  79. while (t < 0.5f) {
  80. t = (ragdoll.RootRigidbody.velocity.magnitude > 0.09f) ? 0 : t + Time.deltaTime;
  81. yield return null;
  82. }
  83. StartCoroutine(Restore());
  84. }
  85. }
  86. }