Hello everyone!
So recently, while working at our game for a school project (A 2D platformer game). Me and my partner ran into a problem with our random level generator. When we play our random generated level, we sometimes run into invisible walls. Our character shows the animation for riding, but he doesn’t move. Only when we redirect the player or jump, the invisible wall stops blocking us and we can move on, until we run into yet another wall in less then 10 seconds. We don’t want this to happen anymore and i couldn’t find any existing threats on it so i thought i’d ask here.
To be clear, we also have normal levels, which we build ourselfs from scratch, and we don’t seem to have this problem in these levels. So we thought the problem would lie with the random level generator’s code.
Here is our code as it is now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapGenerator : MonoBehaviour {
public GameObject groundTop, groundMid, bridge, spikes;
public int minPlatformSize = 1;
public int maxPlatformSize = 10;
public int maxHazardSize = 3;
public int minHazardSize = 2;
public int maxHeight = 3;
public int maxDrop = -3;
public int platforms = 100;
[Range(0.0f, 1f)]
public float hazardChance = .5f;
[Range(0.0f, 1f)]
public float bridgeChance = .1f;
[Range(0.0f, 1f)]
public float spikeChange = .5f;
private int blockNum = 1;
private int blockHeight;
private bool isHazard;
// Use this for initialization
void Start ()
{
//Beginning tile
Instantiate(groundTop, new Vector2(0,0), Quaternion.identity);
for (int plat = 1; plat < platforms; plat++)
{
if (isHazard == true)
{
isHazard = false;
}
else
{
if (Random.value < hazardChance)
{
isHazard = true;
}
else
{
isHazard = false;
}
}
//Hazard generation
if (isHazard ==true)
{
int hazardSize = Mathf.RoundToInt(Random.Range(minHazardSize, maxHazardSize));
if (Random.value < spikeChange)
{
//Generate spike trap
for (int tiles = 0; tiles < hazardSize; tiles++)
{
Instantiate(spikes, new Vector2(blockNum, blockHeight - 2), Quaternion.identity);
for (int grdMid = 1; grdMid < 10; grdMid++)
{
Instantiate(groundMid, new Vector2(blockNum, (blockHeight - grdMid) - 2), Quaternion.identity);
}
blockNum++;
}
}
else
{
//Hole in the ground
blockNum += hazardSize;
}
}
else
{
if (Random.value < bridgeChance)
{
//Bridge generation
int platformSize = Mathf.RoundToInt(Random.Range(minPlatformSize, maxPlatformSize));
blockHeight = blockHeight + Random.Range(maxDrop, maxHeight);
for (int tiles = 0; tiles < platformSize; tiles++)
{
if (tiles == 0 || tiles == platformSize - 1)
{
Instantiate(groundTop, new Vector2(blockNum, blockHeight), Quaternion.identity);
for (int grdMid = 1; grdMid < 10; grdMid++)
{
Instantiate(groundMid, new Vector2(blockNum, blockHeight - grdMid), Quaternion.identity);
}
blockNum++;
}
else
{
Instantiate(bridge, new Vector2(blockNum, blockHeight), Quaternion.identity);
blockNum++;
}
}
}
else
{
//Platform generation
int platformSize = Mathf.RoundToInt(Random.Range(minPlatformSize, maxPlatformSize));
blockHeight = blockHeight + Random.Range(maxDrop, maxHeight);
for (int tiles = 0; tiles < platformSize; tiles++)
{
Instantiate(groundTop, new Vector2(blockNum, blockHeight), Quaternion.identity);
for (int grdMid = 1; grdMid < 10; grdMid++)
{
Instantiate(groundMid, new Vector2(blockNum, blockHeight - grdMid), Quaternion.identity);
}
//Object placement
//if (tiles > 0 && tiles < platformSize - 1)
{
if (Random.value < 0.1f)
{
GetComponent<ObjectPlacement>().PlaceObject(new Vector2(blockNum, blockHeight));
}
blockNum++;
}
}
}
}
}
}
// Update is called once per frame
void Update () {
}
}