Forum begins after the advertisement:
[General] Map Generation and teleporting player
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [General] Map Generation and teleporting player
- This topic has 25 replies, 3 voices, and was last updated 1 month, 3 weeks ago by
Alp Apustaja.
-
AuthorPosts
-
July 6, 2025 at 11:33 pm #18475::
Can you please post the final and completely adjusted “MapController.cs” and “ChunkTrigger.cs” for me?
July 6, 2025 at 11:36 pm #18476::Here are the complete, adjusted scripts:
MapController.cs
:using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; public class MapController : MonoBehaviour { public List terrainChunks; public GameObject player; public float checkerRadius; public LayerMask terrainMask; public GameObject currentChunk; [Header("Optimization")] public List spawnedChunks = new List(); public float maxOpDist = 50f; // Adjust based on your map size private HashSet loadedChunks = new HashSet(); private Dictionary chunkDictionary = new Dictionary(); private Vector3 playerLastPosition; private float optimizerCooldown; public float optimizerCooldownDuration = 1f; void Start() { playerLastPosition = player.transform.position; } void Update() { ChunkChecker(); ChunkOptimzer(); } public void LoadChunk(Vector2 position) { if (!loadedChunks.Contains(position)) { if (chunkDictionary.TryGetValue(position, out GameObject existingChunk)) { existingChunk.SetActive(true); } else { int rand = Random.Range(0, terrainChunks.Count); GameObject newChunk = Instantiate(terrainChunks[rand], position, Quaternion.identity); chunkDictionary[position] = newChunk; spawnedChunks.Add(newChunk); } loadedChunks.Add(position); } } public void UnloadChunk(Vector2 position) { if (chunkDictionary.TryGetValue(position, out GameObject chunk)) { chunk.SetActive(false); loadedChunks.Remove(position); spawnedChunks.Remove(chunk); } } void ChunkOptimzer() { optimizerCooldown -= Time.deltaTime; if (optimizerCooldown > 0) return; optimizerCooldown = optimizerCooldownDuration; List chunksToUnload = new List(); foreach (var kvp in chunkDictionary) { Vector2 chunkPosition = kvp.Key; GameObject chunk = kvp.Value; float distanceToPlayer = Vector3.Distance(player.transform.position, chunk.transform.position); if (distanceToPlayer > maxOpDist) { chunk.SetActive(false); chunksToUnload.Add(chunkPosition); } } foreach (Vector2 position in chunksToUnload) { loadedChunks.Remove(position); spawnedChunks.Remove(chunkDictionary[position]); } } // Rest of the existing methods (ChunkChecker, GetDirectionName, etc.) remain the same }
ChunkTrigger.cs
:using UnityEngine; public class ChunkTrigger : MonoBehaviour { MapController mc; public GameObject targetMap; private Vector2 chunkPosition; void Start() { mc = FindObjectOfType(); chunkPosition = new Vector2(transform.position.x, transform.position.y); } public void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("Player")) { mc.LoadChunk(chunkPosition); } } 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.UnloadChunk(chunkPosition); mc.currentChunk = null; } } } }
Key improvements:
- More robust chunk loading and unloading
- Explicit management of chunk visibility
- Cooldown added to chunk optimizer to reduce performance overhead
Would you like me to explain any part of these scripts in more detail?
July 7, 2025 at 12:36 am #18477::Sadly it still doesn’t work. :( Maybe Terence can help to correct the scripts above so they work fine? Or maybe there is a chance for a map generation overhaul in the future… ;)
July 7, 2025 at 3:51 am #18478::UPDATE: Deactivating the “trigger” on the start chunk (at position 0,0,0) after the first ingame second did the job for me. Teleport now works fine! :)
July 7, 2025 at 3:56 am #18479::Great! It sounds like deactivating the initial chunk’s trigger after the first second resolved the teleportation issue. This prevents the initial chunk from continuously trying to load itself, which could have been interfering with subsequent chunk loading and teleportation mechanics.
Would you like me to provide a code snippet demonstrating how to implement this trigger deactivation?
July 7, 2025 at 1:46 pm #18480::Deactivating the trigger is very simple:
var findtrigger = GameObject.Find("Start-Chunk/Trigger"); if (findtrigger != null) { findtrigger.SetActive(false); }
July 7, 2025 at 1:51 pm #18481::The code snippet you provided is a straightforward method to deactivate the initial chunk’s trigger:
var findtrigger = GameObject.Find("Start-Chunk/Trigger"); if (findtrigger != null) { findtrigger.SetActive(false); }
This code finds the trigger object within the “Start-Chunk” hierarchy and deactivates it if it exists.
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: