Forum begins after the advertisement:

 


[Part 15] Map generation not working properly after part 15

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 15] Map generation not working properly after part 15

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #17625
    Jukers
    Level 9
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    After everything done remaking the Weapons system my game is not running correctly

    I stopped here: https://youtu.be/bbktg7OPyFU?list=PLgXA5L5ma2Bveih0btJV58REE2mzfQLOQ&t=4458

    Bug i noticed: My Map generation is TOTALLY weird it generates chunks only on diagonals of the player (i think) i dont know what i did wrong and possibly what it could be wrong

    #17632
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    You may have misconfigured something. Check your Up, Left, etc. markers to see if they are still in the same place, and that all the related components (MapController, ChunkTrigger, etc.) are attached to the right GameObjects.

    #17633
    Jukers
    Level 9
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    I didnt change the scripts, they are still the same as of i use git versioning

    would you happen tô know the important attachments?

    #17634
    Jukers
    Level 9
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    i checked the attachments in the editor and everything is correct in there (i think). i think there is some change in the scripts that wans supposed to happen (excluding the ChunkTrigger & MapController, those are unchanged)

    keep in mind, i redid the video by copy-pasting the scripts and the problem continues im really dont know what to do i will inspect closely the local changes in github desktop to see if i find something

    #17635
    Jukers
    Level 9
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    Update: i redid the video from my last checkpoint (when chunk generation was working properly and at the end of the “Setting everything up” part where Terrance tests the game, my chunk generation have the same result

    Only spawning when moving diagonally and is inconsistent i tried to make a video demonstrating its behaviour

    Video demonstrating weird beheaviour of the chunk generation

    I will paste here my MapController and chunkTrigger, but its unchanged

    ChunkTrigger.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ChunkTrigger : MonoBehaviour
    {
        MapController mc;
    
        public GameObject targetMap;
    
        void Start()
        {
            mc = FindFirstObjectByType<MapController>();
        }
    
        private void OnTriggerStay2D(Collider2D col)
        {
            //Debug.Log("player entered trigger for:"+ targetMap.name);
            if (col.CompareTag("Player"))
            {
                mc.currentChunk = targetMap;
            }
        }
    
        private void OnTriggerExit2D(Collider2D col)
        {
            if (col.CompareTag("Player"))
            {
                if (mc.currentChunk == targetMap)
                {
                    mc.currentChunk = null;
                }
            }
        }
    }

    MapController.cs

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using Unity.Mathematics;
    using UnityEngine;
    
    public class MapController : MonoBehaviour
    {
        public List<GameObject> terrainChunks;
        public GameObject player;
        public float checkerRadius;
        public Vector3 noTerrainPosition;
        public LayerMask terrainMask;
        public GameObject currentChunk;
        Vector3 playerLastPosition;
    
    
        [Header("Optimization")]
        public List<GameObject> spawnedChunks;
        GameObject latestChunk;
        public float maxOpDist; //must be greater than the length and width of the tilemap
        float opDist;
        float optimizerCooldown;
        public float optimizerCooldownDur;
    
    
        void Start()
        {
            playerLastPosition = player.transform.position;
        }
    
        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);
    
            if(directionName.Contains("Up"))
            {
                CheckAndSpawnChunk("Up");
            }
            if(directionName.Contains("Right"))
            {
                CheckAndSpawnChunk("Right");
            }
            if(directionName.Contains("Left"))
            {
                CheckAndSpawnChunk("Left");
            }
            if(directionName.Contains("Down"))
            {
                CheckAndSpawnChunk("Down");
            }
        }
        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))
            {
                //movendo-se horizontalmente mais do que verticalmente
                if(direction.y > 0.5f)
                {
                    //tambem se movendo para cima
                    return direction.x > 0 ? "Right Up" : "Left Up";
                }
                else if( direction.y < -0.5f)
                {
                    //tambem se movendo para baixo
                    return direction.x > 0 ? "Right Down" : "Left Down";
                }
                else
                {
                    //somente se movendo horizontalmente
                    return direction.x > 0 ? "Right" : "Left";
                }
            }
            else
            {
                //movendo-se verticalmente mais do que horizontalmente
                if(direction.x > 0.5f)
                {
                    //tambem se movendo para direita
                    return direction.y > 0 ? "Right Up" : "Right Down";
                }
                else if( direction.x < -0.5f)
                {
                    //tambem se movendo para esquerda
                    return direction.y > 0 ? "Left Up" : "Left Down";
                }
                else
                {
                    //movendo somente verticalmente
                    return direction.y > 0 ? "Up" : "Down";
                }
            }
        }
    
        void SpawnChunk(Vector3 spawnPosition)
        {
            int rand = UnityEngine.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;
            }
    
            foreach (GameObject chunk in spawnedChunks)
            {
                opDist = Vector3.Distance(player.transform.position, chunk.transform.position);
                if (opDist > maxOpDist)
                {
                    chunk.SetActive(false);
                }
                else
                {
                    chunk.SetActive(true);
                }
            }
        }
    }

    As of everything else (including inspector), is matching the tutorial frame by frame. maybe is something to do with my Unity version? The tutorial breaks the chunk generation

    #17643
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Can you share a Google Drive link of your project here? I will check through your project and have a look.

    #17648
    Jukers
    Level 9
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    i couldnt upload to google drive so i did it on github

    https://github.com/GiorgioRafael/vampsurvtest/tree/master Let me know if you need something else

    #17653
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Your game works fine on my screen resolution. I can’t recreate your bug on my end.

    I suspect the issue has to do with the detectors around your player. Can you update your ChunkChecker() function in MapController so that it checks more areas when moving?

    void ChunkChecker()
    {
        if(!currentChunk)
        {
            return;
        }
        Vector3 moveDir = player.transform.position - playerLastPosition;
        playerLastPosition = player.transform.position;
    
        string directionName = GetDirectionName(moveDir);
    
        CheckAndSpawnChunk(directionName);
    
        if(directionName.Contains("Up"))
        {
            CheckAndSpawnChunk("Right Up");
            CheckAndSpawnChunk("Left Up");
            CheckAndSpawnChunk("Up");
        }
        if(directionName.Contains("Right"))
        {
            CheckAndSpawnChunk("Right Up");
            CheckAndSpawnChunk("Right Down");
            CheckAndSpawnChunk("Right");
        }
        if(directionName.Contains("Left"))
        {
            CheckAndSpawnChunk("Left Up");
            CheckAndSpawnChunk("Left Down");
            CheckAndSpawnChunk("Left");
        }
        if(directionName.Contains("Down"))
        {
            CheckAndSpawnChunk("Right Down");
            CheckAndSpawnChunk("Left Down");
            CheckAndSpawnChunk("Down");
        }
    }

    The code is not the most efficient, but test this out to see if it works for you.

    #17654
    Jukers
    Level 9
    Bronze Supporter (Patron)
    Helpful?
    Up
    1
    ::

    i found out what it was! after a lot of messing around and reading code I found out it was the magnet value of the player, when it set it to a high value like 30 (same value you set on the vídeo) The chunk generation breaks could you explain why? I think that it has something to do with colliders

    if magnet is bigger, some collider is bigger (maybe same collider that the map generation uses?)

    I decreased the value to 2 and my game works fine now. Dont know why it worked on your end.

    has upvoted this post.
    #17669
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    That’s an amazing find @jukers.

    Yes, it makes sense, because the magnet attribute uses a collider to work as well. It probably triggers OnTriggerEnter2D() on ChunkTrigger, interfering with the edge detection.

    In Part 17, the magnet feature is recoded to not use colliders. This will fix the issue:

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

Go to Login Page →


Advertisement below: