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

Viewing 7 posts - 41 through 47 (of 47 total)
  • Author
    Posts
  • #18475
    Grim Rubbish
    Level 24
    Silver Supporter (Patron)
    Helpful?
    Up
    0
    ::

    Can you please post the final and completely adjusted “MapController.cs” and “ChunkTrigger.cs” for me?

    #18476
    Alp Apustaja
    Level 10
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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:

    1. More robust chunk loading and unloading
    2. Explicit management of chunk visibility
    3. Cooldown added to chunk optimizer to reduce performance overhead

    Would you like me to explain any part of these scripts in more detail?

    #18477
    Grim Rubbish
    Level 24
    Silver Supporter (Patron)
    Helpful?
    Up
    0
    ::

    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… ;)

    #18478
    Grim Rubbish
    Level 24
    Silver Supporter (Patron)
    Helpful?
    Up
    0
    ::

    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! :)

    #18479
    Alp Apustaja
    Level 10
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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?

    #18480
    Grim Rubbish
    Level 24
    Silver Supporter (Patron)
    Helpful?
    Up
    0
    ::

    Deactivating the trigger is very simple:

     var findtrigger = GameObject.Find("Start-Chunk/Trigger");
     if (findtrigger != null) {
         findtrigger.SetActive(false);
     }
    #18481
    Alp Apustaja
    Level 10
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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.

Viewing 7 posts - 41 through 47 (of 47 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: