- Home /
Question by
Pervenc · May 01, 2018 at 04:56 PM ·
2dinstantiate2d-platformerplatformgeneration
Platforms are been generated more and more to the left or right
So, my platform generator is generating plataforms more and more to the left or right every time a plataform is generated (after a while platforms wont generate on my screen), and i only want to generate platforms on my screen width (6 units) (I will make the platform genator create platforms as the player progress, but for now i need to solve this)
The code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlatformGen : MonoBehaviour {
public GameObject PlatformPrefab;
public int numberOfPlatforms = 50;
public float levelWidth = 1f;
public float minY = 0.5f;
public float maxY = 2f;
void Start () {
Vector3 spawnPosition = new Vector3();
for (int i = 0; i < numberOfPlatforms; i++)
{
spawnPosition.y += Random.Range(minY, maxY);
spawnPosition.x += Random.Range(-levelWidth, levelWidth);
Instantiate(PlatformPrefab, spawnPosition, Quaternion.identity);
spawnPosition.y += Random.Range(minY, maxY);
spawnPosition.x += Random.Range(-levelWidth, levelWidth);
Instantiate(PlatformPrefab, spawnPosition, Quaternion.identity);
}
}
}
Comment