BasicPlatformerController.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 = 1.5f;
  42. public float runSpeed = 7f;
  43. public float gravityScale = 6.6f;
  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 SkeletonAnimation skeletonAnimation;
  52. [Header("Animation")]
  53. [SpineAnimation(dataField: "skeletonAnimation")]
  54. public string walkName = "Walk";
  55. [SpineAnimation(dataField: "skeletonAnimation")]
  56. public string runName = "Run";
  57. [SpineAnimation(dataField: "skeletonAnimation")]
  58. public string idleName = "Idle";
  59. [SpineAnimation(dataField: "skeletonAnimation")]
  60. public string jumpName = "Jump";
  61. [SpineAnimation(dataField: "skeletonAnimation")]
  62. public string fallName = "Fall";
  63. [SpineAnimation(dataField: "skeletonAnimation")]
  64. public string crouchName = "Crouch";
  65. [Header("Effects")]
  66. public AudioSource jumpAudioSource;
  67. public AudioSource hardfallAudioSource;
  68. public AudioSource footstepAudioSource;
  69. public ParticleSystem landParticles;
  70. [SpineEvent]
  71. public string footstepEventName = "Footstep";
  72. CharacterController controller;
  73. Vector3 velocity = default(Vector3);
  74. float jumpEndTime = 0;
  75. bool jumpInterrupt = false;
  76. float forceCrouchEndTime;
  77. Vector2 input;
  78. bool wasGrounded = false;
  79. void Awake () {
  80. controller = GetComponent<CharacterController>();
  81. }
  82. void Start () {
  83. skeletonAnimation.AnimationState.Event += HandleEvent;
  84. }
  85. void HandleEvent (Spine.TrackEntry trackEntry, Spine.Event e) {
  86. if (e.Data.Name == footstepEventName) {
  87. footstepAudioSource.Stop();
  88. footstepAudioSource.pitch = GetRandomPitch(0.2f);
  89. footstepAudioSource.Play();
  90. }
  91. }
  92. static float GetRandomPitch (float maxOffset) {
  93. return 1f + Random.Range(-maxOffset, maxOffset);
  94. }
  95. void Update () {
  96. input.x = Input.GetAxis(XAxis);
  97. input.y = Input.GetAxis(YAxis);
  98. bool crouching = (controller.isGrounded && input.y < -0.5f) || (forceCrouchEndTime > Time.time);
  99. velocity.x = 0;
  100. float dt = Time.deltaTime;
  101. if (!crouching) {
  102. if (Input.GetButtonDown(JumpButton) && controller.isGrounded) {
  103. jumpAudioSource.Stop();
  104. jumpAudioSource.Play();
  105. velocity.y = jumpSpeed;
  106. jumpEndTime = Time.time + jumpDuration;
  107. } else {
  108. jumpInterrupt |= Time.time < jumpEndTime && Input.GetButtonUp(JumpButton);
  109. }
  110. if (input.x != 0) {
  111. velocity.x = Mathf.Abs(input.x) > 0.6f ? runSpeed : walkSpeed;
  112. velocity.x *= Mathf.Sign(input.x);
  113. }
  114. if (jumpInterrupt) {
  115. if (velocity.y > 0) {
  116. velocity.y = Mathf.MoveTowards(velocity.y, 0, dt * jumpInterruptFactor);
  117. } else {
  118. jumpInterrupt = false;
  119. }
  120. }
  121. }
  122. var gravityDeltaVelocity = Physics.gravity * gravityScale * dt;
  123. if (controller.isGrounded) {
  124. jumpInterrupt = false;
  125. } else {
  126. if (wasGrounded) {
  127. if (velocity.y < 0)
  128. velocity.y = 0;
  129. } else {
  130. velocity += gravityDeltaVelocity;
  131. }
  132. }
  133. wasGrounded = controller.isGrounded;
  134. controller.Move(velocity * dt);
  135. if (!wasGrounded && controller.isGrounded) {
  136. if (-velocity.y > forceCrouchVelocity) {
  137. forceCrouchEndTime = Time.time + forceCrouchDuration;
  138. hardfallAudioSource.Play();
  139. } else {
  140. footstepAudioSource.Play();
  141. }
  142. landParticles.Emit((int)(velocity.y / -9f) + 2);
  143. }
  144. if (controller.isGrounded) {
  145. if (crouching) {
  146. skeletonAnimation.AnimationName = crouchName;
  147. } else {
  148. if (input.x == 0)
  149. skeletonAnimation.AnimationName = idleName;
  150. else
  151. skeletonAnimation.AnimationName = Mathf.Abs(input.x) > 0.6f ? runName : walkName;
  152. }
  153. } else {
  154. skeletonAnimation.AnimationName = velocity.y > 0 ? jumpName : fallName;
  155. }
  156. if (input.x != 0)
  157. skeletonAnimation.Skeleton.FlipX = input.x < 0;
  158. }
  159. }
  160. }