BasicPlatformerController.cs 5.1 KB

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