BasicPlatformerController.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using Spine.Unity;
  30. using UnityEngine;
  31. using UnityEngine.Events;
  32. namespace Spine.Unity.Examples {
  33. [RequireComponent(typeof(CharacterController))]
  34. public class BasicPlatformerController : MonoBehaviour {
  35. public enum CharacterState {
  36. None,
  37. Idle,
  38. Walk,
  39. Run,
  40. Crouch,
  41. Rise,
  42. Fall,
  43. Attack
  44. }
  45. [Header("Components")]
  46. public CharacterController controller;
  47. [Header("Controls")]
  48. public string XAxis = "Horizontal";
  49. public string YAxis = "Vertical";
  50. public string JumpButton = "Jump";
  51. [Header("Moving")]
  52. public float walkSpeed = 1.5f;
  53. public float runSpeed = 7f;
  54. public float gravityScale = 6.6f;
  55. [Header("Jumping")]
  56. public float jumpSpeed = 25;
  57. public float minimumJumpDuration = 0.5f;
  58. public float jumpInterruptFactor = 0.5f;
  59. public float forceCrouchVelocity = 25;
  60. public float forceCrouchDuration = 0.5f;
  61. [Header("Animation")]
  62. public SkeletonAnimationHandleExample animationHandle;
  63. // Events
  64. public event UnityAction OnJump, OnLand, OnHardLand;
  65. Vector2 input = default(Vector2);
  66. Vector3 velocity = default(Vector3);
  67. float minimumJumpEndTime = 0;
  68. float forceCrouchEndTime;
  69. bool wasGrounded = false;
  70. CharacterState previousState, currentState;
  71. void Update () {
  72. float dt = Time.deltaTime;
  73. bool isGrounded = controller.isGrounded;
  74. bool landed = !wasGrounded && isGrounded;
  75. // Dummy input.
  76. input.x = Input.GetAxis(XAxis);
  77. input.y = Input.GetAxis(YAxis);
  78. bool inputJumpStop = Input.GetButtonUp(JumpButton);
  79. bool inputJumpStart = Input.GetButtonDown(JumpButton);
  80. bool doCrouch = (isGrounded && input.y < -0.5f) || (forceCrouchEndTime > Time.time);
  81. bool doJumpInterrupt = false;
  82. bool doJump = false;
  83. bool hardLand = false;
  84. if (landed) {
  85. if (-velocity.y > forceCrouchVelocity) {
  86. hardLand = true;
  87. doCrouch = true;
  88. forceCrouchEndTime = Time.time + forceCrouchDuration;
  89. }
  90. }
  91. if (!doCrouch) {
  92. if (isGrounded) {
  93. if (inputJumpStart) {
  94. doJump = true;
  95. }
  96. } else {
  97. doJumpInterrupt = inputJumpStop && Time.time < minimumJumpEndTime;
  98. }
  99. }
  100. // Dummy physics and controller using UnityEngine.CharacterController.
  101. Vector3 gravityDeltaVelocity = Physics.gravity * gravityScale * dt;
  102. if (doJump) {
  103. velocity.y = jumpSpeed;
  104. minimumJumpEndTime = Time.time + minimumJumpDuration;
  105. } else if (doJumpInterrupt) {
  106. if (velocity.y > 0)
  107. velocity.y *= jumpInterruptFactor;
  108. }
  109. velocity.x = 0;
  110. if (!doCrouch) {
  111. if (input.x != 0) {
  112. velocity.x = Mathf.Abs(input.x) > 0.6f ? runSpeed : walkSpeed;
  113. velocity.x *= Mathf.Sign(input.x);
  114. }
  115. }
  116. if (!isGrounded) {
  117. if (wasGrounded) {
  118. if (velocity.y < 0)
  119. velocity.y = 0;
  120. } else {
  121. velocity += gravityDeltaVelocity;
  122. }
  123. }
  124. controller.Move(velocity * dt);
  125. wasGrounded = isGrounded;
  126. // Determine and store character state
  127. if (isGrounded) {
  128. if (doCrouch) {
  129. currentState = CharacterState.Crouch;
  130. } else {
  131. if (input.x == 0)
  132. currentState = CharacterState.Idle;
  133. else
  134. currentState = Mathf.Abs(input.x) > 0.6f ? CharacterState.Run : CharacterState.Walk;
  135. }
  136. } else {
  137. currentState = velocity.y > 0 ? CharacterState.Rise : CharacterState.Fall;
  138. }
  139. bool stateChanged = previousState != currentState;
  140. previousState = currentState;
  141. // Animation
  142. // Do not modify character parameters or state in this phase. Just read them.
  143. // Detect changes in state, and communicate with animation handle if it changes.
  144. if (stateChanged)
  145. HandleStateChanged();
  146. if (input.x != 0)
  147. animationHandle.SetFlip(input.x);
  148. // Fire events.
  149. if (doJump) {
  150. OnJump.Invoke();
  151. }
  152. if (landed) {
  153. if (hardLand) {
  154. OnHardLand.Invoke();
  155. } else {
  156. OnLand.Invoke();
  157. }
  158. }
  159. }
  160. void HandleStateChanged () {
  161. // When the state changes, notify the animation handle of the new state.
  162. string stateName = null;
  163. switch (currentState) {
  164. case CharacterState.Idle:
  165. stateName = "idle";
  166. break;
  167. case CharacterState.Walk:
  168. stateName = "walk";
  169. break;
  170. case CharacterState.Run:
  171. stateName = "run";
  172. break;
  173. case CharacterState.Crouch:
  174. stateName = "crouch";
  175. break;
  176. case CharacterState.Rise:
  177. stateName = "rise";
  178. break;
  179. case CharacterState.Fall:
  180. stateName = "fall";
  181. break;
  182. case CharacterState.Attack:
  183. stateName = "attack";
  184. break;
  185. default:
  186. break;
  187. }
  188. animationHandle.PlayAnimationForState(stateName, 0);
  189. }
  190. }
  191. }