Forum begins after the advertisement:


[Part 2] Chunks spawning problem

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 2] Chunks spawning problem

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #15334
    Arthur Esteves
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    Hi, i post a comment on the second video of the series of making a vampire survivior game.

    In the part of making the chunks spawning infinite, im having a problem, as you can see in the image bellow, a download your own script and put on the “Map Controller” game object.

    View post on imgur.com

    But when i play the game, the chunks not spawing.

    Can you help me, im probably doing something wrong

    Thank you.

    #15335
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Hi Arthur, can you post your MapController script here and use ScreenToGif to record your screen when you are playing the game and moving the character around to try and spawn the chunks? Make sure you capture the Hierarchy window as well.

    #15337
    Arthur Esteves
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    Hi, this is the MapController script:

    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 = FindObjectOfType<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);
            latestChunk = Instantiate(terrainChunks[rand], noTerrainPosition, Quaternion.identity);
            spawnedChunks.Add(latestChunk);
        }
    
        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);
                }
            }
        }
    }
    #15338
    Arthur Esteves
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    And he’s the gif

    View post on imgur.com
    #15343
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    The SpawnChunk() does not seem to be firing. Let’s add the following lines into your MapController, then try moving around and see if any messages appear on your Console.

        void ChunkChecker()
        {
            if(!currentChunk)
            {
                return;
            }
    
            if (pm.moveDir.x > 0 && pm.moveDir.y == 0)
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(20, 0, 0), checkerRadius, terrainMask))
                {
                    print("SpawnChunk right");
                    noTerrainPosition = player.transform.position + new Vector3(20, 0, 0);  //Right
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.x < 0 && pm.moveDir.y == 0)
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(-20, 0, 0), checkerRadius, terrainMask))
                {
                    print("SpawnChunk left");
                    noTerrainPosition = player.transform.position + new Vector3(-20, 0, 0);    //Left
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.y > 0 && pm.moveDir.x == 0)
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(0, 20, 0), checkerRadius, terrainMask))
                {
                    print("SpawnChunk up");
                    noTerrainPosition = player.transform.position + new Vector3(0, 20, 0); //Up
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.y < 0 && pm.moveDir.x == 0)
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(0, -20, 0), checkerRadius, terrainMask))
                {
                    print("SpawnChunk down");
                    noTerrainPosition = player.transform.position + new Vector3(0, -20, 0);    //Down
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.x > 0 && pm.moveDir.y > 0)
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(20, 20, 0), checkerRadius, terrainMask))
                {
                    print("SpawnChunk up right");
                    noTerrainPosition = player.transform.position + new Vector3(20, 20, 0);   //Right up
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.x > 0 && pm.moveDir.y < 0)
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(20, -20, 0), checkerRadius, terrainMask))
                {
                    print("SpawnChunk down right");
                    noTerrainPosition = player.transform.position + new Vector3(20, -20, 0);  //Right down
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.x < 0 && pm.moveDir.y > 0)
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(-20, 20, 0), checkerRadius, terrainMask))
                {
                    print("SpawnChunk up left");
                    noTerrainPosition = player.transform.position + new Vector3(-20, 20, 0);  //Left up
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.x < 0 && pm.moveDir.y < 0)
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(-20, -20, 0), checkerRadius, terrainMask))
                {
                    print("SpawnChunk down left");
                    noTerrainPosition = player.transform.position + new Vector3(-20, -20, 0); //Left down
                    SpawnChunk();
                }
            }
        }

    Also, can you check if you have a trigger collider in each of your chunk prefabs?

    #15392
    Arthur Esteves
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    Hi Terance!

    I put your lines on the script and trigger the collider on the chunks prefabs, but still don´t work

    the script looked like this:

    <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 = FindObjectOfType<PlayerMovement>();
        }
    
        void Update()
        {
            ChunkChecker();
            ChunkOptimzer();
        }
    
        void ChunkChecker()
        {
            if(!currentChunk)
            {
                return;
            }
    
            if (pm.moveDir.x > 0 && pm.moveDir.y == 0)
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(20, 0, 0), checkerRadius, terrainMask))
                {
                    print("SpawnChunk right");
                    noTerrainPosition = player.transform.position + new Vector3(20, 0, 0);  //Right
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.x < 0 && pm.moveDir.y == 0)
            {
                if(!Physics2D.OverlapCircle(player.transform.position + new Vector3(-20, 0, 0), checkerRadius, terrainMask))
                {
                    print("SpawnChunk left");
                    noTerrainPosition = player.transform.position + new Vector3(-20, 0, 0);    //Left
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.y > 0 && pm.moveDir.x == 0)
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(0, 20, 0), checkerRadius, terrainMask))
                {
                    print("SpawnChunk up");
                    noTerrainPosition = player.transform.position + new Vector3(0, 20, 0); //Up
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.y < 0 && pm.moveDir.x == 0)
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(0, -20, 0), checkerRadius, terrainMask))
                {
                    print("SpawnChunk down");
                    noTerrainPosition = player.transform.position + new Vector3(0, -20, 0);    //Down
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.x > 0 && pm.moveDir.y > 0)
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(20, 20, 0), checkerRadius, terrainMask))
                {
                    print("SpawnChunk up right");
                    noTerrainPosition = player.transform.position + new Vector3(20, 20, 0);   //Right up
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.x > 0 && pm.moveDir.y < 0)
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(20, -20, 0), checkerRadius, terrainMask))
                {
                    print("SpawnChunk down right");
                    noTerrainPosition = player.transform.position + new Vector3(20, -20, 0);  //Right down
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.x < 0 && pm.moveDir.y > 0)
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(-20, 20, 0), checkerRadius, terrainMask))
                {
                    print("SpawnChunk up left");
                    noTerrainPosition = player.transform.position + new Vector3(-20, 20, 0);  //Left up
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.x < 0 && pm.moveDir.y < 0)
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(-20, -20, 0), checkerRadius, terrainMask))
                {
                    print("SpawnChunk down left");
                    noTerrainPosition = player.transform.position + new Vector3(-20, -20, 0); //Left down
                    SpawnChunk();
                }
            }
        }
    
        void SpawnChunk()
        {
            int rand = Random.Range(0, terrainChunks.Count);
            latestChunk = Instantiate(terrainChunks[rand], noTerrainPosition, Quaternion.identity);
            spawnedChunks.Add(latestChunk);
        }
    
        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);
                }
            }
        }
    }</code>

    Thanks for your attention

    #15398
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Did you assign the Terrain Mask variable in the Map Controller component on the Inspector?

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: