Forum begins after the advertisement:
[Part 2] Rapid chunk generation
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 2] Rapid chunk generation
- This topic has 2 replies, 3 voices, and was last updated 4 days, 23 hours ago by
Alp Apustaja.
-
AuthorPosts
-
April 15, 2025 at 1:12 am #17908::
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
April 15, 2025 at 1:20 am #17909::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:
- Add a public GameObject currentChunk variable
- Add a null check for currentChunk at the start of ChunkChecker()
- 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?
April 15, 2025 at 11:36 am #17921::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.
April 16, 2025 at 11:50 pm #17936::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”
April 16, 2025 at 11:51 pm #17937::Based on the reference information, here are the steps to set the Terrain Mask:
- In the Inspector window of the Map Controller object
- Find the Terrain Mask field
- Click the small circle/dot next to the Terrain Mask field
- 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?
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: