- Home /
Question by
MilKID · Jan 30, 2017 at 07:35 AM ·
spawning problemsobject position
Endless Jumper Platform Spawning Script
I have made this script for infinite Jumper game and I want the platforms to spawn according to the camera height in random positions and to the get deactived again to reduce lag.
But I cant get the platforms to actually spawn. If anybody could maybe look over my script and tell me what i am doing wrong it would be very nice.
ublic class Platform
{
public GameObject platformObj;
public string platformType;
public Platform (GameObject obj, string t)
{
platformObj = obj;
platformType = t;
}
}
public class PlatformManagerScript : MonoBehaviour
{
//PROPERTIES
[Header("Platform Prefabs")]
public GameObject[] platform;
List<GameObject> platformBank = new List<GameObject>();
[Header("Number of Platforms in Screen at once")]
public int maxPlatforms = 100;
public float spawnPlatformsTo;
public float platformOffset;
[Header("Screenlimitations")]
public float screenBorderLeft;
public float screenBorderRight;
[HideInInspector]
private float platformCheck;
[HideInInspector]
public Transform player;
[HideInInspector]
public float playerHeightY;
void Start ()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
int numCopies = maxPlatforms * 3;
for (int i = 0; i < numCopies; i++)
{
for (int j = 0; j < platform.Length; j++)
{
GameObject o = (GameObject) Instantiate(platform[j], new Vector3(-20,-20,0),platform[j].transform.rotation);
o.SetActive(false);
platformBank.Add(o);
Debug.Log("Banked Platforms");
}
}
ShuffleList();
}
void Update ()
{
playerHeightY = player.position.y;
if (platformCheck < playerHeightY)
PlatformPositionManager ();
UnactivatePlatform ();
}
void ShuffleList ()
{
System.Random rand = new System.Random ();
int r = platformBank.Count;
while (r > 1)
{
r--;
int n = rand.Next(r+1);
GameObject val = platformBank[n];
platformBank[n] = platformBank[r];
platformBank[r] = val;
}
}
void PlatformPositionManager ()
{
platformCheck = player.position.y + platformOffset;
PlatformSpawner(platformCheck + platformOffset);
}
void PlatformSpawner (float floatValue)
{
float y = spawnPlatformsTo + 2.5f;
while (playerHeightY > 0) {
float x = Random.Range (-2.78f, 0.14f);
Vector3 posXY = new Vector3 (x, y, 0);
for (int n = 0; n < platformBank.Count; n++) {
GameObject o = platformBank [n];
if (!o.activeSelf)
{
o.transform.position = posXY;
o.SetActive(true);
n = platformBank.Count + 1;
}
}
y += Random.Range(1f, 4f);
Debug.Log ("Moved Platform");
}
spawnPlatformsTo = floatValue;
}
void UnactivatePlatform ()
{
foreach (GameObject platform in platformBank) {
if (platform.transform.position.y < playerHeightY - 15)
{
platform.SetActive(false);
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
spawning problem 1 Answer
How can i spawn a prefab at an empty game object? C# 3 Answers
How to reference an array of waypoints to a enemy (clones)? 0 Answers
Trying to get my spawn manager to respond to the current score 1 Answer
[Solved] Detecting objects with OverlapSphere within the frame they are created? 1 Answer