Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class NewGameLogic : MonoBehaviour {
- public GameObject coinPrefab;
- public int maxNumberOfTarget;
- public float minDisWithPlayer, maxDisWithPlayer;
- public Transform[] coins;
- public Vector3[] nextPos;
- public bool isBallStop;
- void Start () {
- isBallStop = true;
- coins = new Transform[maxNumberOfTarget+1];
- nextPos = new Vector3[maxNumberOfTarget];
- coins[0] = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
- //Debug.Log(coins[0].position);
- // Generate new coin
- for (int i=1; i < coins.Length; i++)
- {
- bool inRange = false;
- Vector3 p = new Vector3(0, 0, 0);
- while (!inRange)
- {
- // Compare with player
- inRange = true;
- float x = Random.Range(minDisWithPlayer - maxDisWithPlayer, maxDisWithPlayer - minDisWithPlayer);
- x = x > 0 ? x + minDisWithPlayer : x - minDisWithPlayer;
- float z = Random.Range(minDisWithPlayer - maxDisWithPlayer, maxDisWithPlayer - minDisWithPlayer);
- z = z > 0 ? z + minDisWithPlayer : z - minDisWithPlayer;
- p = new Vector3(x, 1.39f, z);
- float dis = Vector3.Distance(p, coins[0].position);
- if (dis < minDisWithPlayer || dis > maxDisWithPlayer)
- {
- inRange = false;
- break;
- }
- }
- nextPos[i - 1] = p;
- GameObject go = Instantiate(coinPrefab, p, Quaternion.identity);
- go.name = i.ToString();
- coins[i] = go.transform;
- }
- }
- private void Update()
- {
- // Move light ball to new position
- bool stopFlag = true;
- for (int i = 1; i < coins.Length; i++)
- {
- if (Vector3.Distance(nextPos[i - 1], coins[i].transform.position) > 0.0001f)
- {
- stopFlag = false;
- coins[i].transform.position = Vector3.Lerp(coins[i].transform.position, nextPos[i - 1], 0.1f);
- }
- }
- isBallStop = stopFlag;
- }
- private void OnTriggerEnter(Collider other)
- {
- if (other.tag == "Target" && isBallStop)
- {
- isBallStop = false;
- // Generate new coin position
- for (int i = 1; i < coins.Length; i++)
- {
- bool inRange = false;
- Vector3 p = new Vector3(0, 0, 0);
- while (!inRange)
- {
- // Compare with player and other targets
- inRange = true;
- float x = Random.Range(minDisWithPlayer - maxDisWithPlayer, maxDisWithPlayer - minDisWithPlayer);
- x = x > 0 ? x + minDisWithPlayer : x - minDisWithPlayer;
- float z = Random.Range(minDisWithPlayer - maxDisWithPlayer, maxDisWithPlayer - minDisWithPlayer);
- z = z > 0 ? z + minDisWithPlayer : z - minDisWithPlayer;
- p = new Vector3(x, 1.39f, z);
- float dis = Vector3.Distance(p, coins[0].position);
- if (dis < minDisWithPlayer || dis > maxDisWithPlayer)
- {
- inRange = false;
- break;
- }
- }
- nextPos[i-1] = p;
- }
- }
- // int i = int.Parse(other.name);
- // bool inRange = false;
- // Vector3 p = new Vector3(0, 0, 0);
- // while (!inRange)
- // {
- // // Compare with player and other targets
- // inRange = true;
- // float x = Random.Range(minDisWithPlayer - maxDisWithPlayer, maxDisWithPlayer - minDisWithPlayer);
- // x = x > 0 ? x + minDisWithPlayer : x - minDisWithPlayer;
- // float z = Random.Range(minDisWithPlayer - maxDisWithPlayer, maxDisWithPlayer - minDisWithPlayer);
- // z = z > 0 ? z + minDisWithPlayer : z - minDisWithPlayer;
- // p = new Vector3(x, 1.39f, z);
- // for (int j = 0; j < coins.Length; j++)
- // {
- // if (j == i) continue;
- // Debug.Log("(j): " + j);
- // float dis = Vector3.Distance(p, coins[j].position);
- // Debug.Log("dis: " + dis);
- // if(j==0 && (dis < minDisWithPlayer || dis > maxDisWithPlayer))
- // {
- // inRange = false;
- // Debug.Log("------------------------------------");
- // break;
- // }
- // if (dis < minDisWithPlayer || dis > maxDisWithTarget)
- // {
- // inRange = false;
- // Debug.Log("------------------------------------");
- // break;
- // }
- // }
- // }
- // coins[i].position = p;
- //}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement