Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMotor : MonoBehaviour
  6. {
  7. private const float LANE_DISTANCE = 3.0f;
  8. private const float TURN_SPEED = 0.05f;
  9.  
  10. //
  11. //
  12. private bool isRunning = false;
  13. //animation
  14. private Animator anim;
  15.  
  16. //movement
  17. private CharacterController controller;
  18. private float jumpforce = 6.0f;
  19. private float gravity = 12.0f;
  20. private float verticalvelocity;
  21. private int desiredLane = 1; // 0=left, 1=middle, 2 = right
  22.  
  23. //speed modifier
  24. private float originalSpeed = 7.0f;
  25. private float speed;
  26. private float speedIncreaseLastTick;
  27. private float speedIncreaseTime = 2.5f;
  28. private float speedIncreaseAmount = 0.1f;
  29.  
  30. private void Start()
  31. {
  32. speed = originalSpeed;
  33. controller = GetComponent<CharacterController>();
  34. anim = GetComponent<Animator> ();
  35. }
  36.  
  37. private void Update()
  38. {
  39. if (!isRunning)
  40. return;
  41.  
  42. if (Time.time - speedIncreaseLastTick > speedIncreaseTime)
  43. {
  44. speedIncreaseLastTick = Time.time;
  45. speed += speedIncreaseAmount;
  46. GameManager.Instance.UpdateModifier (speed - originalSpeed);
  47. }
  48.  
  49. //gather the inputs on which lane we should be
  50. if (MobileInput.Instance.SwipeLeft)
  51. MoveLane (false);
  52. if (MobileInput.Instance.SwipeRight)
  53. MoveLane (true);
  54.  
  55. // calculate where we should be in the future
  56. Vector3 targetPosition = transform.position.z * Vector3.forward;
  57. if (desiredLane == 0)
  58. targetPosition += Vector3.left * LANE_DISTANCE;
  59. else if (desiredLane == 2)
  60. targetPosition += Vector3.right * LANE_DISTANCE;
  61.  
  62. // move delta calculate
  63. Vector3 moveVector = Vector3.zero;
  64. moveVector.x = (targetPosition - transform.position).normalized.x * speed;
  65.  
  66. bool isGrounded = IsGrounded();
  67. anim.SetBool("Grounded",isGrounded);
  68.  
  69. //calculate Y
  70. if (IsGrounded ()) { //if grounded
  71. verticalvelocity = -0.1f;
  72.  
  73. if (MobileInput.Instance.SwipeUp) {
  74. //jump
  75. anim.SetTrigger ("Jump");
  76. verticalvelocity = jumpforce;
  77. } else if (MobileInput.Instance.SwipeDown)
  78. {
  79. //slide
  80. StartSliding();
  81. Invoke ("StopSliding", 1.0f);
  82.  
  83. }
  84.  
  85. }
  86. else
  87. {
  88. verticalvelocity -= (gravity * Time.deltaTime);
  89.  
  90. // fast falling mechanic
  91. if (MobileInput.Instance.SwipeDown)
  92. {
  93. verticalvelocity = -jumpforce;
  94. }
  95.  
  96.  
  97. }
  98.  
  99. moveVector.y = verticalvelocity;
  100. //moveVector.z = speed;
  101.  
  102. //move the ninja
  103. controller.Move(moveVector * Time.deltaTime);
  104.  
  105. // rotate ninja where he is
  106. Vector3 dir = controller.velocity;
  107. if (dir != Vector3.zero) {
  108. dir.y = 0;
  109. transform.forward = Vector3.Lerp (transform.forward, dir, TURN_SPEED);
  110. }
  111. }
  112.  
  113. private void StartSliding()
  114. {
  115. anim.SetBool ("Sliding",true);
  116. controller.height /= 2;
  117. controller.center = new Vector3 (controller.center.x, controller.center.y / 2, controller.center.z);
  118. }
  119.  
  120. private void StopSliding()
  121. {
  122. anim.SetBool ("Sliding",false);
  123. controller.height *= 2;
  124. controller.center = new Vector3 (controller.center.x, controller.center.y * 2, controller.center.z);
  125. }
  126.  
  127. private void MoveLane(bool goingRight)
  128. {
  129. desiredLane += (goingRight) ? 1 : -1;
  130. desiredLane = Mathf.Clamp (desiredLane, 0, 2);
  131.  
  132. }
  133.  
  134. private bool IsGrounded()
  135. {
  136. Ray groundRay = new Ray (new Vector3 (controller.bounds.center.x,(controller.bounds.center.y - controller.bounds.extents.y) + 0.2f,controller.bounds.center.z),Vector3.down);
  137. Debug.DrawRay (groundRay.origin, groundRay.direction, Color.cyan, 1.0f);
  138.  
  139. return (Physics.Raycast (groundRay, 0.2f + 0.1f));
  140.  
  141. }
  142.  
  143. public void StartRunning()
  144. {
  145. isRunning = true;
  146. anim.SetTrigger ("StartRunning");
  147. }
  148.  
  149. private void Crash()
  150. {
  151. anim.SetTrigger ("Death");
  152. isRunning = false;
  153. }
  154.  
  155. private void OnControllerColliderHit(ControllerColliderHit hit)
  156. {
  157. switch (hit.gameObject.tag)
  158. {
  159. case "Obstacle":
  160. Crash ();
  161. break;
  162. }
  163. }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement