Forum begins after the advertisement:


[Part 2] MapController not updating the current chunk

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 2] MapController not updating the current chunk

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #17217
    anon (Æternus Lux)
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    Hello I am having an Issue with part 2 of the tutorial where the map controller is not updating the current chunk when spawning new chunks causing chunks to cease spawning after the first chunks surrounding the starting chunk have been spawned as far as I can tell my code is identical to the source material other than I am using a player action map for the movement system which as far as I know should not cause any issues but I am stating it anyway. this is my map controller code and my chunk trigger code below also I have confirmed that all settings in unity are the same as the source material so I am relatively sure the problem is somewhere in the code

    edit i forgot to mention while i was trying to debug I changed the ontriggerstay to on trigger enter but the code has the same issue regardless of which one I use

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class MapController : MonoBehaviour
    {
        public List<GameObject> terrainChunks;
        public GameObject player;
        public float checkerRadius;
        Vector3 noTerrainPosition;
        public LayerMask terrainMask;
        public GameObject currentChunk;
        Player pm;
    
    
        // Start is called before the first frame update
        void Start()
        {
            pm = FindObjectOfType<Player>();
        }
    
        // Update is called once per frame
        void Update()
        {
            ChunkChecker();
            if(!currentChunk)
            {
                return;
            }
        }
        public void ChunkChecker()
        {
            Debug.Log("ChunkChecker called. currentChunk is:" + (currentChunk != null ? currentChunk.name : "null"));
            if (!currentChunk)
            {
                return;
            }
            if (pm.MoveInput.x > 0 && pm.MoveInput.y == 0) //right
            {
                if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right").position, checkerRadius, terrainMask))
                {
                    noTerrainPosition = currentChunk.transform.Find("Right").position;
                    SpawnChunk();
                }
            }
            else if (pm.MoveInput.x < 0 && pm.MoveInput.y == 0) //left
            {
                if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left").position, checkerRadius, terrainMask))
                {
                    noTerrainPosition = currentChunk.transform.Find("Left").position;
                    SpawnChunk();
                }
            }
            else if (pm.MoveInput.x == 0 && pm.MoveInput.y > 0) //up
            {
                if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Up").position, checkerRadius, terrainMask))
                {
                    noTerrainPosition = currentChunk.transform.Find("Up").position;
                    SpawnChunk();
                }
            }
            else if (pm.MoveInput.x == 0 && pm.MoveInput.y < 0) //down
            {
                if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Down").position, checkerRadius, terrainMask))
                {
                    noTerrainPosition = currentChunk.transform.Find("Down").position;
                    SpawnChunk();
                }
            }
            else if (pm.MoveInput.x > 0 && pm.MoveInput.y > 0) //right up
            {
                if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right Up").position, checkerRadius, terrainMask))
                {
                    noTerrainPosition = currentChunk.transform.Find("Right Up").position;
                    SpawnChunk();
                }
            }
            else if (pm.MoveInput.x > 0 && pm.MoveInput.y < 0) //right down
            {
                if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right Down").position, checkerRadius, terrainMask))
                {
                    noTerrainPosition = currentChunk.transform.Find("Right Down").position;
                    SpawnChunk();
                }
            }
            else if (pm.MoveInput.x < 0 && pm.MoveInput.y > 0) //left up
            {
                if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left Up").position, checkerRadius, terrainMask))
                {
                    noTerrainPosition = currentChunk.transform.Find("Left Up").position;
                    SpawnChunk();
                }
            }
            else if (pm.MoveInput.x < 0 && pm.MoveInput.y < 0) //left down
            {
                if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left Down").position, checkerRadius, terrainMask))
                {
                    noTerrainPosition = currentChunk.transform.Find("Left Down").position;
                    SpawnChunk();
                }
            }
        }
        void SpawnChunk()
        {
            int rand = Random.Range(0, terrainChunks.Count);
            Instantiate(terrainChunks[rand], noTerrainPosition, Quaternion.identity);
        }
    }```
    

    using System.Collections; using System.Collections.Generic; using UnityEngine;

    public class ChunkTrigger : MonoBehaviour { MapController mc; public GameObject targetMap;

    // Start is called before the first frame update
    void Start()
    {
        mc = FindObjectOfType<MapController>();
        if (mc == null )
        {
            Debug.Log("map controller not found!");
        }
    }
    
    private void OnTriggerEnter2D(Collider2D col) // Use OnTriggerEnter2D
    {
        if (col.CompareTag("Player") && mc != null)
        {
            Debug.Log("player entered trigger for:"+ targetMap.name);
            mc.currentChunk = targetMap; // Set currentChunk
            Debug.Log("currentChunk is now:" + mc.currentChunk.name);
            mc.ChunkChecker(); // Call ChunkChecker immediately
        }
    }
    
    private void OnTriggerExit2D(Collider2D col)
    {
        if (col.CompareTag("Player") && mc != null && mc.currentChunk == targetMap)
        {
            // Did NOT set currentChunk to null here.
            // Let the MapController handle the case where there is no current chunk.
        }
    }

    }“`

    and for reference here is my player controller code

    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.EventSystems;
    using UnityEngine.InputSystem;
    
    public class Player : MonoBehaviour
    {
    
        private PlayerInputMap _input;
        public Vector2 MoveInput { get; private set; }
        // Start is called before the first frame update
        void Start()
        {
            _input = new PlayerInputMap();
            _input.Player.Enable();
    
        }
    
        // Update is called once per frame
        void Update()
        {
            MoveInput = _input.Player.Walk.ReadValue<Vector2>();
    
            transform.Translate(MoveInput * Time.deltaTime *8);
        }
    }```
    #17218
    Lukas Kazda
    Level 14
    Gold Supporter (Patron)
    Helpful?
    Up
    1
    ::

    Hi, i had the same issue as you, and i think to remember it was a editor problem. I would double check all prefabs, the ones that you already have dragged into your world as well as the saved prefabs. Cant find any difference in your code compared to mine

    has upvoted this post.
    #17219
    Lukas Kazda
    Level 14
    Gold Supporter (Patron)
    Helpful?
    Up
    1
    ::

    Another thing i can think of is a missing boxcollider (not set to trigger) on your player object

    has upvoted this post.
    #17229
    anon (Æternus Lux)
    Level 4
    Participant
    Helpful?
    Up
    1
    ::

    I’ll take another look I’m not sure if I checked the prefabs so hopefully that is the problem

    has upvoted this post.
    #17230
    anon (Æternus Lux)
    Level 4
    Participant
    Helpful?
    Up
    1
    ::

    I was missing the rigidbody2d lol thanks for the help you got me looking at the right part of the inspector to see what i missed

    has upvoted this post.
    #17231
    Lukas Kazda
    Level 14
    Gold Supporter (Patron)
    Helpful?
    Up
    1
    ::

    Tag and Boxcollider2D on player gameobject fine as well?

    has upvoted this post.
    #17232
    Lukas Kazda
    Level 14
    Gold Supporter (Patron)
    Helpful?
    Up
    1
    ::

    “Debug.Log(“player entered trigger for:”+ targetMap.name);” Does this line print its output to the console when entering a new chunk? in chunktrigger script in ontriggerenter function

    has upvoted this post.
    #17236
    anon (Æternus Lux)
    Level 4
    Participant
    Helpful?
    Up
    1
    ::

    turns out I was missing the rigidbody 2d so you pretty much hit the nail on the head I only noticed it because you had me check the trigger on the box collider lol I forgot to add one I went back and watched his player set up and checked everything he did and I missed that part while I was sipping my coffee i guess lol

    has upvoted this post.
    #17296
    Jukers
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    I think i have the same problem, but my player configuration is correct. any ideas of what it could be?

    #17297
    Lukas Kazda
    Level 14
    Gold Supporter (Patron)
    Helpful?
    Up
    1
    ::

    Hi Jukers, can you add the Debug.Log() Lines in your Chunktrigger script, in your OnTriggerEnter2D function? and tell us pls if it prints anything on the console

    
    private void OnTriggerEnter2D(Collider2D col) // Use OnTriggerEnter2D
    {
        if (col.CompareTag("Player") && mc != null)
        {
            Debug.Log("player entered trigger for:"+ targetMap.name); //add this for debugging
            mc.currentChunk = targetMap; // Set currentChunk
            Debug.Log("currentChunk is now:" + mc.currentChunk.name); //add this for debugging
            mc.ChunkChecker(); // Call ChunkChecker immediately
        }
    }
    
    has upvoted this post.
    #17300
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    @jukers if you can share your code as well, it will be great.

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

Go to Login Page →


Advertisement below: