BasicPlatformerController.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 AnimationReferenceAsset walk;
  53. public AnimationReferenceAsset run;
  54. public AnimationReferenceAsset idle;
  55. public AnimationReferenceAsset jump;
  56. public AnimationReferenceAsset fall;
  57. public AnimationReferenceAsset crouch;
  58. public AnimationReferenceAsset runFromFall;
  59. [Header("Effects")]
  60. public AudioSource jumpAudioSource;
  61. public AudioSource hardfallAudioSource;
  62. public ParticleSystem landParticles;
  63. public HandleEventWithAudioExample footstepHandler;
  64. CharacterController controller;
  65. Vector2 input = default(Vector2);
  66. Vector3 velocity = default(Vector3);
  67. float minimumJumpEndTime = 0;
  68. float forceCrouchEndTime;
  69. bool wasGrounded = false;
  70. AnimationReferenceAsset targetAnimation;
  71. AnimationReferenceAsset previousTargetAnimation;
  72. void Awake () {
  73. controller = GetComponent<CharacterController>();
  74. }
  75. void Update () {
  76. float dt = Time.deltaTime;
  77. bool isGrounded = controller.isGrounded;
  78. bool landed = !wasGrounded && isGrounded;
  79. // Dummy input.
  80. input.x = Input.GetAxis(XAxis);
  81. input.y = Input.GetAxis(YAxis);
  82. bool inputJumpStop = Input.GetButtonUp(JumpButton);
  83. bool inputJumpStart = Input.GetButtonDown(JumpButton);
  84. bool doCrouch = (isGrounded && input.y < -0.5f) || (forceCrouchEndTime > Time.time);
  85. bool doJumpInterrupt = false;
  86. bool doJump = false;
  87. bool hardLand = false;
  88. if (landed) {
  89. if (-velocity.y > forceCrouchVelocity) {
  90. hardLand = true;
  91. doCrouch = true;
  92. forceCrouchEndTime = Time.time + forceCrouchDuration;
  93. }
  94. }
  95. if (!doCrouch) {
  96. if (isGrounded) {
  97. if (inputJumpStart) {
  98. doJump = true;
  99. }
  100. } else {
  101. doJumpInterrupt = inputJumpStop && Time.time < minimumJumpEndTime;
  102. }
  103. }
  104. // Dummy physics and controller using UnityEngine.CharacterController.
  105. Vector3 gravityDeltaVelocity = Physics.gravity * gravityScale * dt;
  106. if (doJump) {
  107. velocity.y = jumpSpeed;
  108. minimumJumpEndTime = Time.time + minimumJumpDuration;
  109. } else if (doJumpInterrupt) {
  110. if (velocity.y > 0)
  111. velocity.y *= jumpInterruptFactor;
  112. }
  113. velocity.x = 0;
  114. if (!doCrouch) {
  115. if (input.x != 0) {
  116. velocity.x = Mathf.Abs(input.x) > 0.6f ? runSpeed : walkSpeed;
  117. velocity.x *= Mathf.Sign(input.x);
  118. }
  119. }
  120. if (!isGrounded) {
  121. if (wasGrounded) {
  122. if (velocity.y < 0)
  123. velocity.y = 0;
  124. } else {
  125. velocity += gravityDeltaVelocity;
  126. }
  127. }
  128. controller.Move(velocity * dt);
  129. // Animation
  130. if (isGrounded) {
  131. if (doCrouch) {
  132. targetAnimation = crouch;
  133. } else {
  134. if (input.x == 0)
  135. targetAnimation = idle;
  136. else
  137. targetAnimation = Mathf.Abs(input.x) > 0.6f ? run : walk;
  138. }
  139. } else {
  140. targetAnimation = velocity.y > 0 ? jump : fall;
  141. }
  142. if (previousTargetAnimation != targetAnimation) {
  143. if (targetAnimation == run && previousTargetAnimation == fall) {
  144. skeletonAnimation.AnimationState.SetAnimation(0, runFromFall, false);
  145. skeletonAnimation.AnimationState.AddAnimation(0, run, true, 0f);
  146. } else {
  147. skeletonAnimation.AnimationState.SetAnimation(0, targetAnimation, true);
  148. }
  149. }
  150. previousTargetAnimation = targetAnimation;
  151. if (input.x != 0)
  152. skeletonAnimation.Skeleton.FlipX = input.x < 0;
  153. // Effects
  154. if (doJump) {
  155. jumpAudioSource.Stop();
  156. jumpAudioSource.Play();
  157. }
  158. if (landed) {
  159. if (hardLand) {
  160. hardfallAudioSource.Play();
  161. } else {
  162. footstepHandler.Play();
  163. }
  164. landParticles.Emit((int)(velocity.y / -9f) + 2);
  165. }
  166. wasGrounded = isGrounded;
  167. }
  168. }
  169. }