Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5.  
  6. public class Controller : MonoBehaviour {
  7.  
  8. public GameObject Road;
  9. public GameObject Coin;
  10. public List<Material> materials = new List<Material>();
  11.  
  12. private List<Transform> Cpoints = new List<Transform>();
  13. private List<GameObject> CurrentRoads = new List<GameObject>();
  14. private List<GameObject> CurrentCoins = new List<GameObject>();
  15. private int CurrentPoint = 1;
  16. private int positionNum = 0;
  17. private int CurrentRoadId = 0;
  18. private int LevelCount = 0;
  19. private float time = 0;
  20. private float distance = 0, nextDistance = 0;
  21. private float speed = 1.5f;
  22. private Transform CenterPoints;
  23.  
  24. void Start () {
  25.  
  26. CreateNewRoad();
  27. }
  28.  
  29. void CreateNewRoad()
  30. {
  31. for (int i = CurrentCoins.Count - 1; i >= 0; i--) {
  32. if(CurrentCoins[i].transform.position.z < this.transform.position.z)
  33. {
  34. DestroyImmediate(CurrentCoins[i]);
  35. CurrentCoins.RemoveAt(i);
  36. }
  37. }
  38. if(CurrentRoadId > 0)
  39. {
  40. if(CurrentRoadId > 1)
  41. {
  42. DestroyImmediate(CurrentRoads[CurrentRoadId - 2]);
  43. CurrentRoads.RemoveAt(0);
  44. CurrentRoadId--;
  45. }
  46.  
  47. CurrentRoads.Add(Instantiate (Road, Cpoints[LevelCount * 19 + LevelCount - 1].position,
  48. Quaternion.identity) as GameObject);
  49. }
  50. else
  51. CurrentRoads.Add(Instantiate (Road, Vector3.zero, Quaternion.identity) as GameObject);
  52.  
  53. CenterPoints = CurrentRoads[CurrentRoadId].transform.FindChild("Plane").FindChild("Points");
  54. for (int i = 1; i < 21; i++)
  55. {
  56. Transform childPoint = CenterPoints.transform.FindChild("P" + i);
  57. Cpoints.Add(childPoint);
  58.  
  59. int typeNum = Random.Range(0, materials.Count);
  60. Vector3 cPOS = Vector3.zero;
  61.  
  62. if(LevelCount > 0)
  63. cPOS = new Vector3(childPoint.transform.position.x + Random.Range(-1, 2) * 1.0f,
  64. childPoint.transform.position.y - 0.5f,
  65. childPoint.transform.position.z);
  66. else
  67. cPOS = new Vector3(Cpoints[i - 1].position.x + Random.Range(-1,2) * 1.0f,Cpoints[i - 1].position.y - 0.5f, Cpoints[i - 1].position.z);
  68.  
  69. GameObject newCoin = (Instantiate(Coin,cPOS,Quaternion.identity) as GameObject);
  70. CurrentCoins.Add(newCoin);
  71. newCoin.transform.Rotate(90,0,90);
  72. newCoin.transform.renderer.material = materials[typeNum];
  73.  
  74. if(typeNum == 0)
  75. newCoin.name = "Yellow";
  76.  
  77. else if(typeNum == 1)
  78. newCoin.name = "Red";
  79.  
  80. else if(typeNum == 2)
  81. newCoin.name = "Blue";
  82.  
  83. else if(typeNum == 3)
  84. newCoin.name = "Black";
  85. }
  86.  
  87.  
  88. }
  89.  
  90. void Update () {
  91. Vector3 startPoint = new Vector3 (Cpoints [CurrentPoint].position.x + distance,
  92. Cpoints [CurrentPoint].position.y - 0.5f,
  93. Cpoints [CurrentPoint].position.z);
  94. Vector3 endPoint = new Vector3 (Cpoints [CurrentPoint + 1].position.x + distance,
  95. Cpoints [CurrentPoint + 1].position.y - 0.5f,
  96. Cpoints [CurrentPoint + 1].position.z);
  97. transform.position = Vector3.Lerp (startPoint, endPoint, time);
  98. transform.LookAt(Vector3.Lerp(Cpoints[CurrentPoint + 1].transform.position,
  99. Cpoints[CurrentPoint + 2].transform.position,time));
  100. time += Time.deltaTime * speed;
  101.  
  102. if(time > 1)
  103. {
  104. time = 0;
  105. CurrentPoint++;
  106.  
  107. if(Cpoints [CurrentPoint].name == "P17")
  108. {
  109. CurrentRoadId++;
  110. LevelCount++;
  111. CreateNewRoad();
  112. }
  113.  
  114. }
  115.  
  116.  
  117. if(Input.GetKeyDown(KeyCode.LeftArrow))
  118. {
  119. if(positionNum != -1)
  120. {
  121. positionNum--;
  122. nextDistance = 1.0f * positionNum;
  123. }
  124. }
  125.  
  126. else if(Input.GetKeyDown(KeyCode.RightArrow))
  127. {
  128. if(positionNum != 1)
  129. {
  130. positionNum++;
  131. nextDistance = 1.0f * positionNum;
  132. }
  133. }
  134.  
  135. distance = Mathf.Lerp (distance, nextDistance, Time.deltaTime * 3 * speed);
  136.  
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement