Advertisement
Guest User

Untitled

a guest
May 25th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class NewGameLogic : MonoBehaviour {
  6.  
  7. public GameObject coinPrefab;
  8. public int maxNumberOfTarget;
  9. public float minDisWithPlayer, maxDisWithPlayer;
  10.  
  11. public Transform[] coins;
  12. public Vector3[] nextPos;
  13. public bool isBallStop;
  14.  
  15. void Start () {
  16. isBallStop = true;
  17.  
  18. coins = new Transform[maxNumberOfTarget+1];
  19. nextPos = new Vector3[maxNumberOfTarget];
  20. coins[0] = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
  21. //Debug.Log(coins[0].position);
  22.  
  23. // Generate new coin
  24. for (int i=1; i < coins.Length; i++)
  25. {
  26. bool inRange = false;
  27. Vector3 p = new Vector3(0, 0, 0);
  28. while (!inRange)
  29. {
  30. // Compare with player
  31. inRange = true;
  32. float x = Random.Range(minDisWithPlayer - maxDisWithPlayer, maxDisWithPlayer - minDisWithPlayer);
  33. x = x > 0 ? x + minDisWithPlayer : x - minDisWithPlayer;
  34. float z = Random.Range(minDisWithPlayer - maxDisWithPlayer, maxDisWithPlayer - minDisWithPlayer);
  35. z = z > 0 ? z + minDisWithPlayer : z - minDisWithPlayer;
  36. p = new Vector3(x, 1.39f, z);
  37.  
  38. float dis = Vector3.Distance(p, coins[0].position);
  39. if (dis < minDisWithPlayer || dis > maxDisWithPlayer)
  40. {
  41. inRange = false;
  42. break;
  43. }
  44. }
  45. nextPos[i - 1] = p;
  46. GameObject go = Instantiate(coinPrefab, p, Quaternion.identity);
  47. go.name = i.ToString();
  48. coins[i] = go.transform;
  49. }
  50. }
  51.  
  52. private void Update()
  53. {
  54. // Move light ball to new position
  55. bool stopFlag = true;
  56. for (int i = 1; i < coins.Length; i++)
  57. {
  58. if (Vector3.Distance(nextPos[i - 1], coins[i].transform.position) > 0.0001f)
  59. {
  60. stopFlag = false;
  61. coins[i].transform.position = Vector3.Lerp(coins[i].transform.position, nextPos[i - 1], 0.1f);
  62. }
  63. }
  64. isBallStop = stopFlag;
  65. }
  66.  
  67. private void OnTriggerEnter(Collider other)
  68. {
  69. if (other.tag == "Target" && isBallStop)
  70. {
  71. isBallStop = false;
  72.  
  73. // Generate new coin position
  74. for (int i = 1; i < coins.Length; i++)
  75. {
  76. bool inRange = false;
  77. Vector3 p = new Vector3(0, 0, 0);
  78. while (!inRange)
  79. {
  80. // Compare with player and other targets
  81. inRange = true;
  82. float x = Random.Range(minDisWithPlayer - maxDisWithPlayer, maxDisWithPlayer - minDisWithPlayer);
  83. x = x > 0 ? x + minDisWithPlayer : x - minDisWithPlayer;
  84. float z = Random.Range(minDisWithPlayer - maxDisWithPlayer, maxDisWithPlayer - minDisWithPlayer);
  85. z = z > 0 ? z + minDisWithPlayer : z - minDisWithPlayer;
  86. p = new Vector3(x, 1.39f, z);
  87.  
  88. float dis = Vector3.Distance(p, coins[0].position);
  89. if (dis < minDisWithPlayer || dis > maxDisWithPlayer)
  90. {
  91. inRange = false;
  92. break;
  93. }
  94. }
  95. nextPos[i-1] = p;
  96. }
  97. }
  98.  
  99. // int i = int.Parse(other.name);
  100.  
  101.  
  102. // bool inRange = false;
  103. // Vector3 p = new Vector3(0, 0, 0);
  104. // while (!inRange)
  105. // {
  106. // // Compare with player and other targets
  107. // inRange = true;
  108. // float x = Random.Range(minDisWithPlayer - maxDisWithPlayer, maxDisWithPlayer - minDisWithPlayer);
  109. // x = x > 0 ? x + minDisWithPlayer : x - minDisWithPlayer;
  110. // float z = Random.Range(minDisWithPlayer - maxDisWithPlayer, maxDisWithPlayer - minDisWithPlayer);
  111. // z = z > 0 ? z + minDisWithPlayer : z - minDisWithPlayer;
  112. // p = new Vector3(x, 1.39f, z);
  113.  
  114. // for (int j = 0; j < coins.Length; j++)
  115. // {
  116. // if (j == i) continue;
  117.  
  118. // Debug.Log("(j): " + j);
  119. // float dis = Vector3.Distance(p, coins[j].position);
  120. // Debug.Log("dis: " + dis);
  121. // if(j==0 && (dis < minDisWithPlayer || dis > maxDisWithPlayer))
  122. // {
  123. // inRange = false;
  124. // Debug.Log("------------------------------------");
  125. // break;
  126. // }
  127. // if (dis < minDisWithPlayer || dis > maxDisWithTarget)
  128. // {
  129. // inRange = false;
  130. // Debug.Log("------------------------------------");
  131. // break;
  132. // }
  133. // }
  134. // }
  135. // coins[i].position = p;
  136. //}
  137. }
  138.  
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement