BasicPlatformerController.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // Contributed by: Mitch Thompson
  31. using UnityEngine;
  32. using Spine.Unity;
  33. namespace Spine.Unity.Examples {
  34. [RequireComponent(typeof(CharacterController))]
  35. public class BasicPlatformerController : MonoBehaviour {
  36. [Header("Controls")]
  37. public string XAxis = "Horizontal";
  38. public string YAxis = "Vertical";
  39. public string JumpButton = "Jump";
  40. [Header("Moving")]
  41. public float walkSpeed = 4;
  42. public float runSpeed = 10;
  43. public float gravity = 65;
  44. [Header("Jumping")]
  45. public float jumpSpeed = 25;
  46. public float jumpDuration = 0.5f;
  47. public float jumpInterruptFactor = 100;
  48. public float forceCrouchVelocity = 25;
  49. public float forceCrouchDuration = 0.5f;
  50. [Header("Graphics")]
  51. public Transform graphicsRoot;
  52. public SkeletonAnimation skeletonAnimation;
  53. [Header("Animation")]
  54. [SpineAnimation(dataField: "skeletonAnimation")]
  55. public string walkName = "Walk";
  56. [SpineAnimation(dataField: "skeletonAnimation")]
  57. public string runName = "Run";
  58. [SpineAnimation(dataField: "skeletonAnimation")]
  59. public string idleName = "Idle";
  60. [SpineAnimation(dataField: "skeletonAnimation")]
  61. public string jumpName = "Jump";
  62. [SpineAnimation(dataField: "skeletonAnimation")]
  63. public string fallName = "Fall";
  64. [SpineAnimation(dataField: "skeletonAnimation")]
  65. public string crouchName = "Crouch";
  66. [Header("Audio")]
  67. public AudioSource jumpAudioSource;
  68. public AudioSource hardfallAudioSource;
  69. public AudioSource footstepAudioSource;
  70. [SpineEvent]
  71. public string footstepEventName = "Footstep";
  72. CharacterController controller;
  73. Vector2 velocity = Vector2.zero;
  74. Vector2 lastVelocity = Vector2.zero;
  75. bool lastGrounded = false;
  76. float jumpEndTime = 0;
  77. bool jumpInterrupt = false;
  78. float forceCrouchEndTime;
  79. Quaternion flippedRotation = Quaternion.Euler(0, 180, 0);
  80. void Awake () {
  81. controller = GetComponent<CharacterController>();
  82. }
  83. void Start () {
  84. // Register a callback for Spine Events (in this case, Footstep)
  85. skeletonAnimation.AnimationState.Event += HandleEvent;
  86. }
  87. void HandleEvent (Spine.TrackEntry trackEntry, Spine.Event e) {
  88. // Play some sound if footstep event fired
  89. if (e.Data.Name == footstepEventName) {
  90. footstepAudioSource.Stop();
  91. footstepAudioSource.pitch = GetRandomPitch(0.2f);
  92. footstepAudioSource.Play();
  93. }
  94. }
  95. void Update () {
  96. //control inputs
  97. float x = Input.GetAxis(XAxis);
  98. float y = Input.GetAxis(YAxis);
  99. //check for force crouch
  100. bool crouching = (controller.isGrounded && y < -0.5f) || (forceCrouchEndTime > Time.time);
  101. velocity.x = 0;
  102. //Calculate control velocity
  103. if (!crouching) {
  104. if (Input.GetButtonDown(JumpButton) && controller.isGrounded) {
  105. //jump
  106. jumpAudioSource.Stop();
  107. jumpAudioSource.Play();
  108. velocity.y = jumpSpeed;
  109. jumpEndTime = Time.time + jumpDuration;
  110. } else if (Time.time < jumpEndTime && Input.GetButtonUp(JumpButton)) {
  111. jumpInterrupt = true;
  112. }
  113. if (x != 0) {
  114. //walk or run
  115. velocity.x = Mathf.Abs(x) > 0.6f ? runSpeed : walkSpeed;
  116. velocity.x *= Mathf.Sign(x);
  117. }
  118. if (jumpInterrupt) {
  119. //interrupt jump and smoothly cut Y velocity
  120. if (velocity.y > 0) {
  121. velocity.y = Mathf.MoveTowards(velocity.y, 0, Time.deltaTime * 100);
  122. } else {
  123. jumpInterrupt = false;
  124. }
  125. }
  126. }
  127. //apply gravity F = mA (Learn it, love it, live it)
  128. velocity.y -= gravity * Time.deltaTime;
  129. //move
  130. controller.Move(new Vector3(velocity.x, velocity.y, 0) * Time.deltaTime);
  131. if (controller.isGrounded) {
  132. //cancel out Y velocity if on ground
  133. velocity.y = -gravity * Time.deltaTime;
  134. jumpInterrupt = false;
  135. }
  136. Vector2 deltaVelocity = lastVelocity - velocity;
  137. if (!lastGrounded && controller.isGrounded) {
  138. //detect hard fall
  139. if ((gravity * Time.deltaTime) - deltaVelocity.y > forceCrouchVelocity) {
  140. forceCrouchEndTime = Time.time + forceCrouchDuration;
  141. hardfallAudioSource.Play();
  142. } else {
  143. //play footstep audio if light fall because why not
  144. footstepAudioSource.Play();
  145. }
  146. }
  147. //graphics updates
  148. if (controller.isGrounded) {
  149. if (crouching) { //crouch
  150. skeletonAnimation.AnimationName = crouchName;
  151. } else {
  152. if (x == 0) //idle
  153. skeletonAnimation.AnimationName = idleName;
  154. else //move
  155. skeletonAnimation.AnimationName = Mathf.Abs(x) > 0.6f ? runName : walkName;
  156. }
  157. } else {
  158. if (velocity.y > 0) //jump
  159. skeletonAnimation.AnimationName = jumpName;
  160. else //fall
  161. skeletonAnimation.AnimationName = fallName;
  162. }
  163. //flip left or right
  164. if (x > 0)
  165. graphicsRoot.localRotation = Quaternion.identity;
  166. else if (x < 0)
  167. graphicsRoot.localRotation = flippedRotation;
  168. //store previous state
  169. lastVelocity = velocity;
  170. lastGrounded = controller.isGrounded;
  171. }
  172. static float GetRandomPitch (float maxOffset) {
  173. return 1f + Random.Range(-maxOffset, maxOffset);
  174. }
  175. }
  176. }