Здравствуйте! Я начинающий разработчик на Unity. Я делаю бесконечный раннер, в нем есть платформы на разном расстоянии и разной высоте. Нужно сделать так, что монеты спавнились на платформе и спавнились рандомно. Так как я начинающий, то думал скорректировать скрипт спавна платформ. Но ничего не работает. Помогите пожалуйста разобраться. Вот 1 код для спавна платформ.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformGenerator : MonoBehaviour {
public GameObject platform;
public Transform generationPoint;
public float distanceBetween;
public GameObject coin;
public float spawnCoinDistance;
public GameObject player;
float platformWidth;
public float distanceMin;
public float distanceMax;
float minHeight;
public Transform maxHeightPoint;
float maxHeight;
public float maxHeightChange;
float heightChange;
//public GameObject[] platforms;
int platformSelector;
float[] platformsWidth;
public Transform minHeightPoint;
public float minHeightChange;
public PlatformManger[] platformsM;
void Start ()
{
//platformWidth = platform.GetComponent<BoxCollider2D>().size.y;
platformsWidth = new float[platformsM.Length];
for (int i = 0; i < platformsM.Length; i++)
{
platformsWidth[i] = platformsM[i].platform.GetComponent<BoxCollider2D>().size.x;
}
minHeight = transform.position.y;
maxHeight = maxHeightPoint.position.y;
}
void Update ()
{
if (transform.position.x < generationPoint.position.x)
{
spawnCoinDistance = Random.Range (transform.position.x, generationPoint.position.x);
distanceBetween = Random.Range (distanceMin, distanceMax);
platformSelector = Random.Range (0, platformsM.Length);
heightChange = transform.position.y + Random.Range (maxHeightChange, -maxHeightChange);
if (heightChange > maxHeight)
{
heightChange = maxHeight;
}
else if (heightChange < minHeight)
{
heightChange = minHeight;
}
transform.position = new Vector3 (transform.position.x + (platformsWidth[platformSelector]/2) + distanceBetween, heightChange, transform.position.z);
//Instantiate (/*platform*/ platforms[platformSelector], transform.position, transform.rotation);
GameObject newPlatform = platformsM[platformSelector].GetPlatform ();
newPlatform.transform.position = transform.position;
newPlatform.transform.rotation = transform.rotation;
newPlatform.SetActive (true);
Instantiate (coin);
//transform.position = new Vector3 (transform.position.x + (platformsWidth[platformSelector]/2) + distanceBetween, heightChange, transform.position.z);
}
}
}
и 2 код.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformManger : MonoBehaviour {
public GameObject platform;
public GameObject coin;
public int platfotmAmount;
public int coinAmount;
List<GameObject> platforms;
List<GameObject> coins;
void Start () {
platforms = new List<GameObject> ();
coins = new List<GameObject> ();
for (int i = 0; i < coinAmount; i++)
{
GameObject obj = (GameObject)Instantiate (coin);
obj.SetActive (false);
coins.Add (obj);
}
for (int i = 0; i < platfotmAmount; i++)
{
GameObject obj = (GameObject) Instantiate (platform);
obj.SetActive(false);
platforms.Add(obj);
}
}
public GameObject GetPlatform()
{
for (int i = 0; i < platforms.Count; i++)
{
if(!platforms[i].activeInHierarchy)
{
return platforms [i];
}
}
GameObject obj = (GameObject) Instantiate (platform);
obj.SetActive(false);
platforms.Add(obj);
return obj;
}
}