Forum begins after the advertisement:


[Guide] Notable Map Generation Errors

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Guide] Notable Map Generation Errors

Viewing 10 posts - 16 through 25 (of 25 total)
  • Author
    Posts
  • #17235
    Lukas Kazda
    Level 22
    Gold Supporter (Patron)
    Helpful?
    Up
    0
    ::

    awesome!

    #17323
    Alp Apustaja
    Level 7
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Based on the reference information and the user’s description, the solution was already found by the user: they were missing a Rigidbody2D component, which is required for trigger collisions to work properly in Unity.

    The key insight is that for OnTriggerStay2D, OnTriggerEnter2D, and OnTriggerExit2D to function, both the object with the collider and the object entering/exiting the collider need to have a Rigidbody2D component (which can be set to kinematic if you don’t want physics-based movement).

    #17324
    Alp Apustaja
    Level 7
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Hi Lukas! It looks like you’ve got a link to a post about updating the current chunk in a map controller. If you have specific questions or need help with your code related to that topic, feel free to share the details here! I’m here to help!

    #17330
    Alp Apustaja
    Level 7
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Based on the reference information, the solution involves setting the BoxCollider2D on the Terrain Chunk to be a trigger. Specifically, the instructions state:

    “We also need to add a BoxCollider2D and set it to be a trigger and the size should be the same as our tilemap so 20×20. We need this because Physics2D.OverlapCircle requires a collider.”

    So make sure that:

    1. The BoxCollider2D on each Terrain Chunk is set to “Is Trigger = True”
    2. The collider size matches the tilemap (20×20)

    This should allow the player to move freely between chunks without collision blocking movement.

    #17974
    Hiếu Hoàng
    Level 10
    Silver Supporter (Patron)
    Helpful?
    Up
    0
    ::

    hi i am facing an error while following what is in the video. after following what is written on the video, i have an issue where my map controller never gets updated. here is my code here is my chunk trigger code using System.Collections; using System.Collections.Generic; using UnityEngine;

    public class ChunkTrigger : MonoBehaviour { MapController mc;

    public GameObject targetMap;
    
    void Start()
    {
        mc = FindAnyObjectByType<MapController>();
    }
    
    private void OnTriggerStay2D(Collider2D col)
    {
        if (col.CompareTag("Player"))
        {
            mc.currentChunk = targetMap;
        }
    }
    
    private void OnTriggerExit2D(Collider2D col)
    {
        if (col.CompareTag("Player"))
        {
            if (mc.currentChunk == targetMap)
            {
                mc.currentChunk = null;
            }
        }
    }

    } here is my map controller code using System.Collections; using System.Collections.Generic; using UnityEngine;

    public class MapController : MonoBehaviour { public List<GameObject> terrainChunks; public GameObject player; public float checkerRadius; public Vector3 noTerrainPosition; public LayerMask terrainMask; public GameObject currentChunk; PlayerMovement pm;

    [Header("Optimization")]
    public List<GameObject> spawnedChunks;
    GameObject latestChunk;
    public float maxOpDist; //Must be greater than the length and width of the tilemap
    float opDist;
    float optimizerCooldown;
    public float optimizerCooldownDur;
    
    
    
    void Start()
    {
        pm = FindAnyObjectByType<PlayerMovement>();
    }
    
    void Update()
    {
        ChunkChecker();
        ChunkOptimzer();
    }
    
    void ChunkChecker()
    {
        if (!currentChunk)
        {
            return;
        }
    
        if (pm.moveDir.x > 0 && pm.moveDir.y == 0)
        {
            if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right").position, checkerRadius, terrainMask))
            {
                noTerrainPosition = currentChunk.transform.Find("Right").position;  //Right
                SpawnChunk();
            }
        }
        else if (pm.moveDir.x < 0 && pm.moveDir.y == 0)
        {
            if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left").position, checkerRadius, terrainMask))
            {
                noTerrainPosition = currentChunk.transform.Find("Left").position;    //Left
                SpawnChunk();
            }
        }
        else if (pm.moveDir.y > 0 && pm.moveDir.x == 0)
        {
            if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Up").position, checkerRadius, terrainMask))
            {
                noTerrainPosition = currentChunk.transform.Find("Up").position; //Up
                SpawnChunk();
            }
        }
        else if (pm.moveDir.y < 0 && pm.moveDir.x == 0)
        {
            if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Down").position, checkerRadius, terrainMask))
            {
                noTerrainPosition = currentChunk.transform.Find("Down").position;    //Down
                SpawnChunk();
            }
        }
        else if (pm.moveDir.x > 0 && pm.moveDir.y > 0)
        {
            if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right Up").position, checkerRadius, terrainMask))
            {
                noTerrainPosition = currentChunk.transform.Find("Right Up").position;   //Right up
                SpawnChunk();
            }
        }
        else if (pm.moveDir.x > 0 && pm.moveDir.y < 0)
        {
            if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right Down").position, checkerRadius, terrainMask))
            {
                noTerrainPosition = currentChunk.transform.Find("Right Down").position;  //Right down
                SpawnChunk();
            }
        }
        else if (pm.moveDir.x < 0 && pm.moveDir.y > 0)
        {
            if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left Up").position, checkerRadius, terrainMask))
            {
                noTerrainPosition = currentChunk.transform.Find("Left Up").position;  //Left up
                SpawnChunk();
            }
        }
        else if (pm.moveDir.x < 0 && pm.moveDir.y < 0)
        {
            if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left Down").position, checkerRadius, terrainMask))
            {
                noTerrainPosition = currentChunk.transform.Find("Left Down").position; //Left down
                SpawnChunk();
            }
        }
    }
    
    void SpawnChunk()
    {
        int rand = Random.Range(0, terrainChunks.Count);
        Instantiate(terrainChunks[rand], noTerrainPosition, Quaternion.identity);
    
    }
    
    void ChunkOptimzer()
    {
        optimizerCooldown -= Time.deltaTime;
    
        if (optimizerCooldown <= 0f)
        {
            optimizerCooldown = optimizerCooldownDur;   //Check every 1 second to save cost, change this value to lower to check more times
        }
        else
        {
            return;
        }
    
        foreach (GameObject chunk in spawnedChunks)
        {
            opDist = Vector3.Distance(player.transform.position, chunk.transform.position);
            if (opDist > maxOpDist)
            {
                chunk.SetActive(false);
            }
            else
            {
                chunk.SetActive(true);
            }
        }
    }

    } This is the screen after running the game

    View post on imgur.com
    Can anyone help me with this part?
    #17975
    Alp Apustaja
    Level 7
    AI Assistant
    (BETA)
    Helpful?
    Up
    1
    ::

    Based on the reference information, there are a few potential issues:

    1. Ensure the player object’s tag is set to “Player”
    2. Make sure each Terrain Chunk has:
      • A Trigger BoxCollider2D
      • A Rigidbody2D component (can be kinematic)
      • Child objects named “Right”, “Left”, “Up”, “Down”, “Right Up”, “Right Down”, “Left Up”, “Left Down”
    3. In the MapController, verify that:
      • The terrainChunks list is populated
      • The player reference is set
      • The checkerRadius is set to a small value (like 0.2)
      • The terrainMask is set to the Terrain layer

    Could you confirm that you’ve set up all these components exactly as described in the reference information?

    has upvoted this post.
    #17976
    Hiếu Hoàng
    Level 10
    Silver Supporter (Patron)
    Helpful?
    Up
    0
    ::

    I followed all the instructions in the video and checked again but still got errors

    #17977
    Alp Apustaja
    Level 7
    AI Assistant
    (BETA)
    Helpful?
    Up
    1
    ::

    Could you provide more specific details about the errors you’re experiencing? What exact error messages are you seeing, or what specific behavior is not working as expected? The more details you can share, the better I can help diagnose the issue.

    has upvoted this post.
    #18029
    Hiếu Hoàng
    Level 10
    Silver Supporter (Patron)
    Helpful?
    Up
    0
    ::

    I have a problem, can you help me?

    View post on imgur.com
    #18030
    Alp Apustaja
    Level 7
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    It looks like you’re trying to share an image link for context, but I can’t view external links directly. If you could describe the issue or any error messages you’re encountering, that would be really helpful!

    Additionally, please make sure to check the following:

    • Are the currentChunk and targetMap references set correctly?
    • Is the Rigidbody2D on the player and the chunks configured properly?
    • Are you seeing any debug logs in the console when you enter the trigger area?

    Let me know, and I’ll do my best to assist you!

Viewing 10 posts - 16 through 25 (of 25 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: