Forum begins after the advertisement:


[Part 2] Map Generation

Viewing 15 posts - 1 through 15 (of 21 total)
  • Author
    Posts
  • #12244
    sad agent
    Former Patron

    Hello, im on part 2 25:20 and i did everything at map controller script improvement, but there is no more map loading anymore now. I have just the first one to appear, then no others are spawning as im moving.

    ChunkTrigger script:

    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;
                      }
            
                }
            
        }
    }

    Map Controller script:

    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;
        PlayerMovement pm;
    
        void Start()
        {
            pm = FindObjectOfType<PlayerMovement>();
        }
    
        void Update()
        {
            ChunkChecker();
        }
        
        void ChunkChecker()
        {
            if(!currentChunk)    
            {
                return;
            }
    
    
            if(pm.moveDir.x > 0 && pm.moveDir.y == 0) //vpravo
                {
                    if(!Physics2D.OverlapCircle(currentChunk.transform.Find("Right").position, checkerRadius, terrainMask))
                        {
                            noTerrainPosition = currentChunk.transform.Find("Right").position;
                            SpawnChunk();
    
                        }
                }
    
            else if(pm.moveDir.x < 0 && pm.moveDir.y == 0) //vlavo
                {
                    if(!Physics2D.OverlapCircle(currentChunk.transform.Find("Left").position, checkerRadius, terrainMask))
                        {
                            noTerrainPosition = currentChunk.transform.Find("Left").position;
                            SpawnChunk();
    
                        }
            
                }
    
            else if(pm.moveDir.x == 0 && pm.moveDir.y > 0) //hore
                {
                    if(!Physics2D.OverlapCircle(currentChunk.transform.Find("Up").position, checkerRadius, terrainMask))
                        {
                            noTerrainPosition = currentChunk.transform.Find("Up").position;
                            SpawnChunk();
    
                        }
            
                }           
            else if(pm.moveDir.x == 0 && pm.moveDir.y < 0) //dole
                {
                    if(!Physics2D.OverlapCircle(currentChunk.transform.Find("Down").position, checkerRadius, terrainMask))
                        {
                            noTerrainPosition = currentChunk.transform.Find("Down").position;
                            SpawnChunk();
    
                        }
            
                }
    
            else if(pm.moveDir.x > 0 && pm.moveDir.y > 0) //vpravo hore
                {
                    if(!Physics2D.OverlapCircle(currentChunk.transform.Find("Right Up").position, checkerRadius, terrainMask))
                        {
                            noTerrainPosition = currentChunk.transform.Find("Right Up").position;
                            SpawnChunk();
    
                        }
            
                }
    
            else if(pm.moveDir.x > 0 && pm.moveDir.y < 0) //vpravo dole
                {
                    if(!Physics2D.OverlapCircle(currentChunk.transform.Find("Right Down").position, checkerRadius, terrainMask))
                        {
                            noTerrainPosition = currentChunk.transform.Find("Right Down").position;
                            SpawnChunk();
    
                        }
            
                }
    
            else if(pm.moveDir.x < 0 && pm.moveDir.y > 0) //vlavo hore
                {
                    if(!Physics2D.OverlapCircle(currentChunk.transform.Find("Left Up").position, checkerRadius, terrainMask))
                        {
                            noTerrainPosition = currentChunk.transform.Find("Left Up").position;
                            SpawnChunk();
    
                        }
            
                }
    
            else if(pm.moveDir.x < 0 && pm.moveDir.y < 0) //vlavo dole
                {
                    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);
    
        }
    
    }
    #12245
    sad agent
    Former Patron
    #12248
    Terence
    Keymaster

    Hi sad agent, did you miss out assigning any properties on the MapController? You will need to assign the appropriate Terrain Mask, and make sure that the Checker Radius is larger than 0.

    #12250
    sad agent
    Former Patron

    I think they are filled up correcty, and other ideas?

    View post on imgur.com

    #12252
    Terence
    Keymaster

    Try putting a Debug.Log() statement on each of the collision checks on MapController. Here’s an example:

    if(pm.moveDir.x > 0 && pm.moveDir.y == 0) //vpravo
    {
        if(!Physics2D.OverlapCircle(currentChunk.transform.Find("Right").position, checkerRadius, terrainMask))
        {
            Debug.Log("MapController right collision");
            noTerrainPosition = currentChunk.transform.Find("Right").position;
            SpawnChunk();
    
        }
    }

    See if the messages print.

    #12265
    sad agent
    Former Patron

    Where should this message print? Nothing happened when I added this.

    #12266
    sad agent
    Former Patron

    If you mean this console, then there is nothing here

    View post on imgur.com

    #12267
    Terence
    Keymaster

    If nothing happens, it means that either of the 2 lines below are the issue:

    if(pm.moveDir.x > 0 && pm.moveDir.y == 0) //vpravo
    {
        if(!Physics2D.OverlapCircle(currentChunk.transform.Find("Right").position, checkerRadius, terrainMask))
        {
            Debug.Log("MapController right collision");
            noTerrainPosition = currentChunk.transform.Find("Right").position;
            SpawnChunk();
    
        }
    }

    I suspect it is the 2nd highlighted line. Check if your chunk Prefabs have children GameObjects named “Right”, “Left”, “Up”, “Down” etc. The generator finds these objects and uses them to determine when a new chunk should generate.

    #12268
    sad agent
    Former Patron

    it stopped working when i changed cords for currentChunk.transform.Find("Right").position in if(!Physics2D.OverlapCircle(currentChunk.transform.Find("Right").position, checkerRadius, terrainMask))

    #12269
    Terence
    Keymaster

    What do you mean? Where did you change the coords? In the inspector?

    #12271
    sad agent
    Former Patron

    No you were supposed to put actual cords in the code before instead of currentChunk.transform.Find("Right").position etc

    #12272
    sad agent
    Former Patron

    its like new vector3(20, 20, 0) etc

    #12273
    Josh
    Former Patron

    I had the same issue, but I figured out what my problem was.

    Check that your static coordinates (“Up”, “Right”, etc) are placed properly. In my case, my chunks are 84×84, and so I had them placed right at the edges (so “Right” was at (42, 0) and “Left” was at (-42, 0)) but I had to put them at the total length (“Right” = (84, 0), “Left” = (-84, 0)).

    I hope this makes sense and is helpful!

    #12279
    sad agent
    Former Patron

    I am pretty sure I got it correctly, all the gunks are 20×20 pixels, imporing photo of “Right” static point. https://imgur.com/a/Es2et1r

    #12281
    Terence
    Keymaster

    If I’m not wrong, your “Right” has to be right at the edge of your chunk. It’s too far away.

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

Go to Login Page →


Advertisement below: