Advertisement
Guest User

Untitled

a guest
Sep 20th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. // The line below automatically applies a SuperCharacterController to
  5. // the object if it doesn't have it
  6. [RequireComponent(typeof(SuperCharacterController))]
  7. public class PlayerMachine : SuperStateMachine {
  8.  
  9. public float speed = 6.0F;
  10. public float jumpSpeed = 100.0F;
  11. public float gravity = 20.0F;
  12. private Vector3 moveDirection = Vector3.zero;
  13. float hori;
  14. float vert;
  15. float dist;
  16. bool jump;
  17.  
  18. // Add more states by comma separating them
  19. enum PlayerStates { Idle, Jumping, Falling, Running }
  20.  
  21. private SuperCharacterController controller;
  22.  
  23. void Start () {
  24. // Put any code here you want to run ONCE, when the object is initialized
  25.  
  26. // Grab the controller object from our object
  27. controller = gameObject.GetComponent<SuperCharacterController>();
  28.  
  29. // Set our currentState to idle on startup
  30. currentState = PlayerStates.Idle;
  31. }
  32.  
  33. protected override void EarlyGlobalSuperUpdate()
  34. {
  35. // Put any code in here you want to run BEFORE the state's update function.
  36. // This is run regardless of what state you're in
  37.  
  38. hori = Input.GetAxis("Horizontal");
  39. vert = Input.GetAxis("Vertical");
  40. jump = Input.GetButton("Jump");
  41.  
  42. dist = controller.currentGround.Hit.distance;
  43. }
  44.  
  45. protected override void LateGlobalSuperUpdate()
  46. {
  47. // Put any code in here you want to run AFTER the state's update function.
  48. // This is run regardless of what state you're in
  49.  
  50. }
  51.  
  52. /*void Update () {
  53. * Update is normally run once on every frame update. We won't be using it
  54. * in this case, since the SuperCharacterController component sends a callback Update
  55. * called SuperUpdate. SuperUpdate is recieved by the SuperStateMachine, and then fires
  56. * further callbacks depending on the state
  57. }*/
  58.  
  59. // Below are the three state functions. Each one is called based on the name of the state,
  60. // so when currentState = Idle, we call Idle_EnterState. If currentState = Jump, we call
  61. // Jump_SuperUpdate()
  62. void Idle_EnterState()
  63. {
  64. // Run once when we enter the idle state
  65. controller.EnableClamping();
  66.  
  67. }
  68.  
  69. void Idle_SuperUpdate()
  70. {
  71. // Run every frame we are in the idle state
  72. transform.position = new Vector3(0, 0, 0);
  73. }
  74.  
  75. void Idle_ExitState()
  76. {
  77. // Run once when we exit the idle state
  78. }
  79.  
  80. void Jumping_EnterState()
  81. {
  82. // Run once when we enter the idle state
  83. controller.DisableClamping();
  84. }
  85.  
  86. void Jumping_SuperUpdate()
  87. {
  88. // Run every frame we are in the idle state
  89. }
  90.  
  91. void Jumping_ExitState()
  92. {
  93. // Run once when we exit the idle state
  94. }
  95.  
  96. void Falling_EnterState()
  97. {
  98. // Run once when we enter the idle state
  99. controller.DisableClamping();
  100. }
  101.  
  102. void Falling_SuperUpdate()
  103. {
  104. // Run every frame we are in the idle state
  105. }
  106.  
  107. void Falling_ExitState()
  108. {
  109. // Run once when we exit the idle state
  110. }
  111.  
  112. void Running_EnterState()
  113. {
  114. // Run once when we enter the running state
  115. controller.EnableClamping();
  116. }
  117.  
  118. void Running_SuperUpdate()
  119. {
  120. // Run every frame we are in the running state
  121. //moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  122. //moveDirection = transform.TransformDirection(moveDirection);
  123. //moveDirection *= speed;
  124.  
  125. Vector3 moveForward = new Vector3(0, 0, Input.GetAxis("Vertical"));
  126. transform.position += moveForward;
  127.  
  128. if (jump) // Jumping is true
  129. {
  130. currentState = PlayerStates.Jumping;
  131. }
  132.  
  133. if (hori != 0f && dist <= 0.05 || vert != 0f && dist <= 0.05) // Movement in a direction
  134. {
  135. currentState = PlayerStates.Running;
  136. }
  137.  
  138. }
  139.  
  140. void Running_ExitState()
  141. {
  142. // Run once when we exit the running state
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement