Unwanted invisibl walls in random level generator

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 () {
       
    }
}

Hi,
I have to admit I didn’t read your code but from your description this sounds like it might be related to colliders being slightly out of line with each other. Does this issue happen at the intersection of two tiles? Do the individual tiles have individual box colliders?

Hi, sorry for the late reply, i thought that i had already replied to you several days ago.
You are right about the box colliders, there’s an individual box collider for every tile. Do you know if we can even fix this? Cause the tiles (and colliders) should be generated right next to eachother and flow into eachother.

If you are laying your tiles out in advance for each level, then you could look at the composite collider 2d and tilemap collider 2d option to make a single collider around all tiles automatically.

I’m not sure how to do the above for tiles that are being generated and added programatically which is what I think you are doing?

If you can’t find a solution around the above, is it possible you could use EdgeCollider2D on the surface of the tiles your character is walking on?
Alternatively, could you use a capsulecollider2d or circlecollider2d on the player character? This prevents two ‘sharp edges’ meeting each other. (Some side effects may be though that you might find your character bouncing somewhat with this second approach).