Advertisement
Guest User

Untitled

a guest
May 25th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.32 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerController : MonoBehaviour
  5. {
  6.  
  7. public GameObject player;
  8. public ParticleSystem particle;
  9. public GroundController groundController;
  10. public int dirTurn; // This variable make the player turn left or turn right
  11. public bool touchDisable;
  12. public bool isPlayerHitTheWall; // Check player hit the wall
  13. public bool gameOver;
  14. public float movingSpeedOfPlayer = 10f; //How fast player moving
  15. public float movingSpeedIncrement = 0.2f; // How much to increase player speed after each score
  16. public float timeToDestroyParticle = 0.5f; // How long particle survive
  17.  
  18.  
  19.  
  20. private ParticleSystem particleTemp;
  21. private Vector3 dir; // This variable make the player run back when it hit the wall
  22. private bool hittedWallLeft; // Check the wall left is hitted
  23. private bool hittedWallright; // Check the wall right is hitted
  24. // Use this for initialization
  25. void Start()
  26. {
  27. StartCoroutine(MovePlayer());
  28. touchDisable = false;
  29. gameOver = false;
  30. hittedWallLeft = false;
  31. hittedWallright = false;
  32. dirTurn = 1;
  33. }
  34.  
  35. // Update is called once per frame
  36. void Update()
  37. {
  38. // Make the player redirected every time we touch the screen
  39. if (Input.GetMouseButtonDown(0) && groundController.enableTouch && !touchDisable)
  40. {
  41. movingSpeedOfPlayer += movingSpeedIncrement;
  42. touchDisable = true;
  43. dirTurn = dirTurn * (-1);
  44. if (dirTurn < 0)
  45. {
  46. dir = Vector3.forward;
  47. }
  48. else
  49. {
  50. dir = Vector3.right;
  51. }
  52.  
  53. }
  54.  
  55. Ray rayDown = new Ray(player.transform.position, Vector3.down);
  56. RaycastHit hit;
  57.  
  58. if(Physics.Raycast(rayDown,out hit, 0.6f))
  59. {
  60. if (hit.collider.gameObject.tag == "Ground")
  61. {
  62. isPlayerHitTheWall = false;
  63. Ray rayForward = new Ray(player.transform.position, Vector3.forward);
  64. Ray rayBack = new Ray(player.transform.position, Vector3.back);
  65. Ray rayRight = new Ray(player.transform.position, Vector3.right);
  66. Ray rayLeft = new Ray(player.transform.position, Vector3.left);
  67.  
  68. // If player hit the wall, player will run back
  69. // If player hit gold, count gold and destroy it
  70. if (Physics.Raycast(rayForward, out hit, 0.6f))
  71. {
  72. if (hit.collider.tag == "Gold")
  73. {
  74. SoundManager.Instance.PlaySound(SoundManager.Instance.hitCoin);
  75. CoinManager.Instance.AddCoins(1);
  76. particleTemp = (ParticleSystem)Instantiate(particle, hit.collider.gameObject.transform.position, Quaternion.identity);
  77. particleTemp.transform.rotation = Quaternion.Euler(90, 0, 0);
  78. particleTemp.Simulate(0.5f, true, false);
  79. particleTemp.Play();
  80. Destroy(particleTemp, timeToDestroyParticle);
  81. Destroy(hit.collider.gameObject);
  82. }
  83.  
  84. if (hit.collider.tag == "TheWall")
  85. {
  86.  
  87. if (!hittedWallLeft)
  88. {
  89. hittedWallLeft = true;
  90. hittedWallright = false;
  91. ScoreManager.Instance.AddScore(1);
  92. }
  93. isPlayerHitTheWall = true;
  94. touchDisable = false;
  95. dir = Vector3.back;
  96. }
  97. }
  98.  
  99. // If player hit the wall, player will run forward
  100. else if (Physics.Raycast(rayBack, out hit, 0.6f))
  101. {
  102. if (hit.collider.tag == "TheWall")
  103. {
  104.  
  105. dir = Vector3.forward;
  106. }
  107. }
  108.  
  109. // If player hit the wall, player will run left
  110. // If player hit gold, count gold and destroy it
  111. else if (Physics.Raycast(rayRight, out hit, 0.6f))
  112. {
  113. if (hit.collider.tag == "Gold")
  114. {
  115. SoundManager.Instance.PlaySound(SoundManager.Instance.hitCoin);
  116. CoinManager.Instance.AddCoins(1);
  117. particleTemp = (ParticleSystem)Instantiate(particle, hit.collider.gameObject.transform.position, Quaternion.identity);
  118. particleTemp.transform.rotation = Quaternion.Euler(90, 0, 0);
  119. particleTemp.Simulate(0.5f, true, false);
  120. particleTemp.Play();
  121. Destroy(particleTemp, timeToDestroyParticle);
  122. Destroy(hit.collider.gameObject);
  123. }
  124. if (hit.collider.tag == "TheWall")
  125. {
  126.  
  127. if (!hittedWallright)
  128. {
  129. hittedWallright = true;
  130. hittedWallLeft = false;
  131. ScoreManager.Instance.AddScore(1);
  132. }
  133. isPlayerHitTheWall = true;
  134. touchDisable = false;
  135. dir = Vector3.left;
  136. }
  137. }
  138.  
  139. // If player hit the wall, player will run right
  140. else if (Physics.Raycast(rayLeft, out hit, 0.6f))
  141. {
  142. if (hit.collider.tag == "TheWall")
  143. {
  144.  
  145. dir = Vector3.right;
  146. }
  147. }
  148. }
  149.  
  150. }
  151. else
  152. {
  153. if (!gameOver)
  154. {
  155. SoundManager.Instance.PlaySound(SoundManager.Instance.gameOver);
  156. touchDisable = true;
  157. gameOver = true;
  158. dir = Vector3.down + new Vector3(0, -1, 0);
  159. }
  160. }
  161. }
  162.  
  163.  
  164. // If player hit gold(trigger), destroy gold
  165. void OnTriggerEnter(Collider other)
  166. {
  167. SoundManager.Instance.PlaySound(SoundManager.Instance.hitCoin);
  168. CoinManager.Instance.AddCoins(1);
  169. particleTemp = (ParticleSystem)Instantiate(particle, other.gameObject.transform.position, Quaternion.identity);
  170. particleTemp.transform.rotation = Quaternion.Euler(90, 0, 0);
  171. particleTemp.Simulate(0.5f, true, false);
  172. particleTemp.Play();
  173. Destroy(particleTemp, timeToDestroyParticle);
  174. Destroy(other.gameObject);
  175. }
  176.  
  177. void OnBecameInvisible()
  178. {
  179. Destroy(player);
  180. }
  181.  
  182. // This function make player move with direction(dir), speed(movingSpeedOfPlayer) and real time
  183. IEnumerator MovePlayer()
  184. {
  185. while (true)
  186. {
  187. player.transform.position = player.transform.position + dir * movingSpeedOfPlayer * Time.deltaTime;
  188. yield return null;
  189. }
  190. }
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement