- Home /
How to randomly replace the main platform with additional ones?
Hello. I am new to programming. Difficulties arose in creating multiple platforms that would randomly replace the main platform. There are 1 main and 4 additional platforms in total. The problem with this code is that it doesn't work correctly during platform generation. I would be glad for any help. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Spawner : MonoBehaviour
{
public float minX, maxX, YrangeMin, YrangeMax, cameraDistance;
public Transform platformPrefab, movePlatformPrefab, downPlatformPrefab;
public float percentToSpawnStandartPlatform, percentToSpawnDownPlatform;
private Transform cam;
private float lastSpawnY;
private float rangeIncreaser;
void Start()
{
cam = Camera.main.transform;
lastSpawnY = 0;
}
void Update()
{
if (lastSpawnY < 250)
{
rangeIncreaser = Mathf.Floor(lastSpawnY / 50);
}
if (cam.position.y + cameraDistance > lastSpawnY)
{
Transform lawnplatform;
if (Random.value < percentToSpawnStandartPlatform)
lawnplatform = Instantiate(platformPrefab);
else
lawnplatform = Instantiate(movePlatformPrefab);
if (Random.value < percentToSpawnDownPlatform)
lawnplatform = Instantiate(platformPrefab);
else
lawnplatform = Instantiate(downPlatformPrefab);
lawnplatform.position = new Vector3(Random.Range(minX, maxX), lastSpawnY + Random.Range(YrangeMin + (rangeIncreaser * 0.9f), YrangeMax + (rangeIncreaser * 1.1f)), 0);
lastSpawnY = lawnplatform.position.y;
}
}
}
Comment