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
- This topic has 10 replies, 4 voices, and was last updated 2 days, 6 hours ago by
Terence.
-
AuthorPosts
-
February 8, 2025 at 5:12 am #17217::
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); } }```
February 8, 2025 at 1:12 pm #17218February 8, 2025 at 1:21 pm #17219February 9, 2025 at 11:28 am #17229February 9, 2025 at 11:51 am #17230February 9, 2025 at 12:01 pm #17231February 9, 2025 at 12:05 pm #17232February 9, 2025 at 12:26 pm #17236::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. February 19, 2025 at 11:53 am #17296::I think i have the same problem, but my player configuration is correct. any ideas of what it could be?
February 19, 2025 at 5:48 pm #17297::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. February 19, 2025 at 7:39 pm #17300 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: