Forum begins after the advertisement:

 


[Part 2] Rapid chunk generation

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #17908
    Unstable Mechanic
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    I’m having a problem with the code, where if the player moves up, down or any ways diagonally, the chunks will rapidly generate. (I haven’t finished the video I’m still stuck on the map controller script chapter) When I tried to play test, I realized that. (But for some reason going left and right works normally)

    The script on MapController

    using UnityEngine;
    using System.Collections.Generic;
    
    public class MapController : MonoBehaviour
    {
        public List<GameObject> terrainChunks;
        public GameObject player;
        public float checkerRadius;
        Vector3 noTerrainPosition;
        public LayerMask terrainMask;
        PlayerMovement pm;
    
        // Start is called once before the first execution of Update after the MonoBehaviour is created
        void Start()
        {
            pm = FindObjectOfType<PlayerMovement>();    
        }
    
        // Update is called once per frame
        void Update()
        {
            ChunkChecker();
        }
    
        void ChunkChecker()
        {
            if (pm.moveDir.x > 0 && pm.moveDir.y == 0)   //right
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(20, 0, 0), checkerRadius, terrainMask))
                {
                    noTerrainPosition = player.transform.position + new Vector3(20, 0, 0);
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.x < 0 && pm.moveDir.y == 0)   //left
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(-20, 0, 0), checkerRadius, terrainMask))
                {
                    noTerrainPosition = player.transform.position + new Vector3(-20, 0, 0);
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.x == 0 && pm.moveDir.y > 0)   //up
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(0, 20, 0), checkerRadius, terrainMask))
                {
                    noTerrainPosition = player.transform.position + new Vector3(0, 20, 0);
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.x == 0 && pm.moveDir.y < 0)   //down
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(0, -20, 0), checkerRadius, terrainMask))
                {
                    noTerrainPosition = player.transform.position + new Vector3(0, -20, 0);
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.x > 0 && pm.moveDir.y >  0)   //right up
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(20, 20, 0), checkerRadius, terrainMask))
                {
                    noTerrainPosition = player.transform.position + new Vector3(20, 20, 0);
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.x > 0 && pm.moveDir.y < 0)   //right down
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(20, -20, 0), checkerRadius, terrainMask))
                {
                    noTerrainPosition = player.transform.position + new Vector3(20, -20, 0);
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.x < 0 && pm.moveDir.y > 0)   //left up
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(-20, 20, 0), checkerRadius, terrainMask))
                {
                    noTerrainPosition = player.transform.position + new Vector3(-20, 20, 0);
                    SpawnChunk();
                }
            }
            else if (pm.moveDir.x < 0 && pm.moveDir.y < 0)   //left down
            {
                if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(-20, -20, 0), checkerRadius, terrainMask))
                {
                    noTerrainPosition = player.transform.position + new Vector3(-20, -20, 0);
                    SpawnChunk();
                }
            }
        }
    
        void SpawnChunk()
        {
            int rand = Random.Range(0, terrainChunks.Count);
            Instantiate(terrainChunks[rand], noTerrainPosition, Quaternion.identity);
        }
    }
    View post on imgur.com
    #17909
    Alp Apustaja
    Level 3
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Based on the reference information, the solution is to modify the MapController script to use the currentChunk’s specific points for checking terrain generation, instead of using the player’s position directly.

    The key changes would be:

    1. Add a public GameObject currentChunk variable
    2. Add a null check for currentChunk at the start of ChunkChecker()
    3. Replace player.transform.position with currentChunk.transform.Find(“Direction”).position in each condition

    The reference information suggests using transform.Find() to reference specific points on the chunk, which should prevent rapid chunk generation when moving diagonally or vertically.

    Would you like me to provide a more detailed code example of these modifications?

    #17921
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    Can you show what are the variables you’ve selected in Terrain Mask? You need to make sure that the correct layer is selected, and your terrain chunks are tagged under the same layer as well.

    #17936
    Unstable Mechanic
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    I’m not exactly sure how to show you my variables in the terrain mask, because I don’t know where that is. Can you tell my step by step how to find and show my variables in the terrain mask?

    Both of my terrain chunks are in the layer 6: terrain, though.

    (Just a side note, I am a complete beginner and this is my first time using Unity, I do not know any basics, I just decided to use this Vampire Survivor video as my first attempt at using Unity)

    Edit: in my game object for map controller the terrain mask is in fact in the layer “terrain”

    #17937
    Alp Apustaja
    Level 3
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Based on the reference information, here are the steps to set the Terrain Mask:

    1. In the Inspector window of the Map Controller object
    2. Find the Terrain Mask field
    3. Click the small circle/dot next to the Terrain Mask field
    4. In the layer dropdown, select Layer 6: Terrain

    Since you mentioned your terrain chunks are already on layer 6, this should work perfectly. The reference information specifically mentions using User Layer 6 for Terrain.

    Would you like me to confirm which specific steps you might be struggling with in setting the Terrain Mask?

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

Go to Login Page →


Advertisement below: