BasicPlatformerController.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 Spine.Unity;
  32. namespace Spine.Unity.Examples {
  33. [RequireComponent(typeof(CharacterController))]
  34. public class BasicPlatformerController : MonoBehaviour {
  35. [Header("Controls")]
  36. public string XAxis = "Horizontal";
  37. public string YAxis = "Vertical";
  38. public string JumpButton = "Jump";
  39. [Header("Moving")]
  40. public float walkSpeed = 1.5f;
  41. public float runSpeed = 7f;
  42. public float gravityScale = 6.6f;
  43. [Header("Jumping")]
  44. public float jumpSpeed = 25;
  45. public float minimumJumpDuration = 0.5f;
  46. public float jumpInterruptFactor = 0.5f;
  47. public float forceCrouchVelocity = 25;
  48. public float forceCrouchDuration = 0.5f;
  49. [Header("Visuals")]
  50. public SkeletonAnimation skeletonAnimation;
  51. [Header("Animation")]
  52. public TransitionDictionaryExample transitions;
  53. public AnimationReferenceAsset walk;
  54. public AnimationReferenceAsset run;
  55. public AnimationReferenceAsset idle;
  56. public AnimationReferenceAsset jump;
  57. public AnimationReferenceAsset fall;
  58. public AnimationReferenceAsset crouch;
  59. public AnimationReferenceAsset runFromFall;
  60. [Header("Effects")]
  61. public AudioSource jumpAudioSource;
  62. public AudioSource hardfallAudioSource;
  63. public ParticleSystem landParticles;
  64. public HandleEventWithAudioExample footstepHandler;
  65. CharacterController controller;
  66. Vector2 input = default(Vector2);
  67. Vector3 velocity = default(Vector3);
  68. float minimumJumpEndTime = 0;
  69. float forceCrouchEndTime;
  70. bool wasGrounded = false;
  71. AnimationReferenceAsset targetAnimation;
  72. AnimationReferenceAsset previousTargetAnimation;
  73. void Awake () {
  74. controller = GetComponent<CharacterController>();
  75. }
  76. void Update () {
  77. float dt = Time.deltaTime;
  78. bool isGrounded = controller.isGrounded;
  79. bool landed = !wasGrounded && isGrounded;
  80. // Dummy input.
  81. input.x = Input.GetAxis(XAxis);
  82. input.y = Input.GetAxis(YAxis);
  83. bool inputJumpStop = Input.GetButtonUp(JumpButton);
  84. bool inputJumpStart = Input.GetButtonDown(JumpButton);
  85. bool doCrouch = (isGrounded && input.y < -0.5f) || (forceCrouchEndTime > Time.time);
  86. bool doJumpInterrupt = false;
  87. bool doJump = false;
  88. bool hardLand = false;
  89. if (landed) {
  90. if (-velocity.y > forceCrouchVelocity) {
  91. hardLand = true;
  92. doCrouch = true;
  93. forceCrouchEndTime = Time.time + forceCrouchDuration;
  94. }
  95. }
  96. if (!doCrouch) {
  97. if (isGrounded) {
  98. if (inputJumpStart) {
  99. doJump = true;
  100. }
  101. } else {
  102. doJumpInterrupt = inputJumpStop && Time.time < minimumJumpEndTime;
  103. }
  104. }
  105. // Dummy physics and controller using UnityEngine.CharacterController.
  106. Vector3 gravityDeltaVelocity = Physics.gravity * gravityScale * dt;
  107. if (doJump) {
  108. velocity.y = jumpSpeed;
  109. minimumJumpEndTime = Time.time + minimumJumpDuration;
  110. } else if (doJumpInterrupt) {
  111. if (velocity.y > 0)
  112. velocity.y *= jumpInterruptFactor;
  113. }
  114. velocity.x = 0;
  115. if (!doCrouch) {
  116. if (input.x != 0) {
  117. velocity.x = Mathf.Abs(input.x) > 0.6f ? runSpeed : walkSpeed;
  118. velocity.x *= Mathf.Sign(input.x);
  119. }
  120. }
  121. if (!isGrounded) {
  122. if (wasGrounded) {
  123. if (velocity.y < 0)
  124. velocity.y = 0;
  125. } else {
  126. velocity += gravityDeltaVelocity;
  127. }
  128. }
  129. controller.Move(velocity * dt);
  130. // Animation
  131. // Determine target animation.
  132. if (isGrounded) {
  133. if (doCrouch) {
  134. targetAnimation = crouch;
  135. } else {
  136. if (input.x == 0)
  137. targetAnimation = idle;
  138. else
  139. targetAnimation = Mathf.Abs(input.x) > 0.6f ? run : walk;
  140. }
  141. } else {
  142. targetAnimation = velocity.y > 0 ? jump : fall;
  143. }
  144. // Handle change in target animation.
  145. if (previousTargetAnimation != targetAnimation) {
  146. Animation transition = null;
  147. if (transitions != null && previousTargetAnimation != null) {
  148. transition = transitions.GetTransition(previousTargetAnimation, targetAnimation);
  149. }
  150. if (transition != null) {
  151. skeletonAnimation.AnimationState.SetAnimation(0, transition, false).MixDuration = 0.05f;
  152. skeletonAnimation.AnimationState.AddAnimation(0, targetAnimation, true, 0f);
  153. } else {
  154. skeletonAnimation.AnimationState.SetAnimation(0, targetAnimation, true);
  155. }
  156. }
  157. previousTargetAnimation = targetAnimation;
  158. // Face intended direction.
  159. if (input.x != 0)
  160. skeletonAnimation.Skeleton.FlipX = input.x < 0;
  161. // Effects
  162. if (doJump) {
  163. jumpAudioSource.Stop();
  164. jumpAudioSource.Play();
  165. }
  166. if (landed) {
  167. if (hardLand) {
  168. hardfallAudioSource.Play();
  169. } else {
  170. footstepHandler.Play();
  171. }
  172. landParticles.Emit((int)(velocity.y / -9f) + 2);
  173. }
  174. wasGrounded = isGrounded;
  175. }
  176. }
  177. }