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)
- This topic has 8 replies, 2 voices, and was last updated 15 hours, 25 minutes ago by liv.
-
AuthorPosts
-
November 13, 2024 at 9:01 pm #16322::
hi, after the fix from part 11 on the forum, the normal spawn of chunks still does not work
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); } } } }
November 13, 2024 at 9:03 pm #16323November 14, 2024 at 9:07 am #16326::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
November 14, 2024 at 4:42 pm #16332::i used code from this topic but it still didn’t work. it sometimes starts loading when walking in diagonal but not every time.
November 14, 2024 at 10:10 pm #16334November 16, 2024 at 2:36 pm #16360::<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>
November 16, 2024 at 2:39 pm #16361November 17, 2024 at 3:07 pm #16374::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?
November 17, 2024 at 8:08 pm #16379 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: