Forum begins after the advertisement:


[Part 11] Map bug (chunks won’t load)

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 11] Map bug (chunks won’t load)

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #16322
    liv
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    hi, after the fix from part 11 on the forum, the normal spawn of chunks still does not work

    View post on imgur.com

    ChunkTrigger:

    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ChunkTrigger : MonoBehaviour
    {
        MapController mc;
    
        public GameObject targetMap;
    
        void Start()
        {
            mc = FindObjectOfType<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;
                }
            }
        }
    }
    

    MapController:

    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class MapController : MonoBehaviour
    {
        //Store prefabs for the terrainchunks
        public List<GameObject> terrainChunks;
        //references the player
        public GameObject player;
        //checks the radius
        public float checkerRadius;
        //track which layer is the terrain and which is not the terrain
        public LayerMask terrainMask;
        //reference to access variables
        public GameObject currentChunk;
        Vector3 playerLastPosition;
    
        [Header("Optimization")]
        //stores the current chunks
        public List<GameObject> spawnedChunks;
        //last chunk that was spawned
        GameObject latestChunk;
        //Max optimization distance
        //used to set the max distance for each of the chunk from player
        public float maxOpDist; //Must be greater than the length and width of the tilemap
        //references the current distance for each chunks
        float opDist;
        float optimizerCooldown;
        public float optimizerCooldownDur;
    
        // Start is called before the first frame update
        void Start()
        {
            playerLastPosition = player.transform.position;
        }
    
        // Update is called once per frame
        void Update()
        {
            ChunkChecker();
            ChunkOptimizer();
        }
    
        void ChunkChecker()
        {
            if (!currentChunk)
            {
                return;
            }
    
            Vector3 moveDir = player.transform.position - playerLastPosition;
            playerLastPosition = player.transform.position;
    
            string directionName = GetDirectionName(moveDir);
    
            CheckAndSpawnChunk(directionName);
    
            // Check additional adjacent directions for diagonal chunks
            if (directionName.Contains("Up"))
            {
                CheckAndSpawnChunk("Up");
                CheckAndSpawnChunk("Left");
                CheckAndSpawnChunk("Right");
            }
            else if (directionName.Contains("Down"))
            {
                CheckAndSpawnChunk("Down");
                CheckAndSpawnChunk("Left");
                CheckAndSpawnChunk("Right");
            }
            else if (directionName.Contains("Right"))
            {
                CheckAndSpawnChunk("Right");
                CheckAndSpawnChunk("Down");
                CheckAndSpawnChunk("Up");
            }
            else if (directionName.Contains("Left"))
            {
                CheckAndSpawnChunk("Left");
                CheckAndSpawnChunk("Down");
                CheckAndSpawnChunk("Up");
            }
        }
    
        void CheckAndSpawnChunk(string direction)
        {
            if (!Physics2D.OverlapCircle(currentChunk.transform.Find(direction).position, checkerRadius, terrainMask))
            {
                SpawnChunk(currentChunk.transform.Find(direction).position);
            }
        }
    
        string GetDirectionName(Vector3 direction)
        {
            direction = direction.normalized;
    
            if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y))
            {
                //moving horizontally more than vertically
                if (direction.y > 0.5f)
                {
                    //Also moving upwards
                    return direction.x > 0 ? "Right Up" : "Left Up";
                }
                else if (direction.y < -0.5f)
                {
                    //also moving downwards
                    return direction.x > 0 ? "Right Down" : "Left Down";
                }
                else
                {
                    //moving straight horizontally
                    return direction.x > 0 ? "Right" : "Left";
                }
            }
            else
            {
                //moving vertically more than vertically
                if (direction.x > 0.5f)
                {
                    //also moving right
                    return direction.y > 0 ? "Right Up" : "Right Down";
                }
                else if (direction.x < -0.5f)
                {
                    //also moving left
                    return direction.y > 0 ? "Left Up" : "Left Down";
                }
                else
                {
                    //moving straight vertically
                    return direction.y > 0 ? "Up" : "Down";
                }
            }
        }
    
        void SpawnChunk(Vector3 spawnPosition)
        {
            int rand = Random.Range(0, terrainChunks.Count);
            latestChunk = Instantiate(terrainChunks[rand], spawnPosition, Quaternion.identity);
            spawnedChunks.Add(latestChunk);
        }
    
        void ChunkOptimizer()
        {
            optimizerCooldown -= Time.deltaTime;
    
            if (optimizerCooldown <= 0f)
            {
                optimizerCooldown = optimizerCooldownDur;
            }
            else
            {
                return;
            }
    
            //for loop
            foreach (GameObject chunk in spawnedChunks)
            {
                //distance of the chunk that is being checked
                opDist = Vector3.Distance(player.transform.position, chunk.transform.position);
                //checks if chunk dist is more than the max optimization dist and disable it if it is
                if (opDist > maxOpDist)
                {
                    chunk.SetActive(false);
                }
                else
                {
                    chunk.SetActive(true);
                }
            }
        }
    }
    #16323
    liv
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    chunk

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

    liv, can you have a look at this topic and see if it helps you fix your issue?

    [Part 11] Chunk not loading correctly and enemies are multiplied when spawning

    #16332
    liv
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    i used code from this topic but it still didn’t work. it sometimes starts loading when walking in diagonal but not every time.

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

    Can you check if all your chunk prefabs have a chunk trigger properly set up?

    #16360
    liv
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    <script async src=”//s.imgur.com/min/embed.js” charset=”utf-8″></script>

    <script async src=”//s.imgur.com/min/embed.js” charset=”utf-8″></script>

    <script async src=”//s.imgur.com/min/embed.js” charset=”utf-8″></script>

    #16361
    #16374
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    It looks like your 2nd chunk prefab has a smaller than usual collider. Can you try increasing the size of that and see if it fixes the issue?

    #16379
    liv
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    Sorry, but what do you mean by 2nd chunk prefab? I changed all prefabs to 20×20 but it didn’t fix anything. I’m starting to think that the problem may be in code because map spawns when you walk in diagonals.

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

Go to Login Page →


Advertisement below: