Forum begins after the advertisement:
[Part 2] Chunks spawning problem
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 2] Chunks spawning problem
- This topic has 6 replies, 2 voices, and was last updated 3 months ago by Terence.
-
AuthorPosts
-
July 22, 2024 at 12:17 am #15334Arthur EstevesParticipant::
Hi, i post a comment on the second video of the series of making a vampire survivior game.
In the part of making the chunks spawning infinite, im having a problem, as you can see in the image bellow, a download your own script and put on the “Map Controller” game object.
But when i play the game, the chunks not spawing.
Can you help me, im probably doing something wrong
Thank you.
July 22, 2024 at 2:10 pm #15335TerenceKeymaster::Hi Arthur, can you post your MapController script here and use ScreenToGif to record your screen when you are playing the game and moving the character around to try and spawn the chunks? Make sure you capture the Hierarchy window as well.
July 23, 2024 at 8:52 am #15337Arthur EstevesParticipant::Hi, this is the MapController script:
using System.Collections; using System.Collections.Generic; 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; PlayerMovement pm; [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() { pm = FindObjectOfType<PlayerMovement>(); } void Update() { ChunkChecker(); ChunkOptimzer(); } void ChunkChecker() { if(!currentChunk) { return; } if (pm.moveDir.x > 0 && pm.moveDir.y == 0) { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right").position, checkerRadius, terrainMask)) { noTerrainPosition = currentChunk.transform.Find("Right").position; //Right SpawnChunk(); } } else if (pm.moveDir.x < 0 && pm.moveDir.y == 0) { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left").position, checkerRadius, terrainMask)) { noTerrainPosition = currentChunk.transform.Find("Left").position; //Left SpawnChunk(); } } else if (pm.moveDir.y > 0 && pm.moveDir.x == 0) { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Up").position, checkerRadius, terrainMask)) { noTerrainPosition = currentChunk.transform.Find("Up").position; //Up SpawnChunk(); } } else if (pm.moveDir.y < 0 && pm.moveDir.x == 0) { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Down").position, checkerRadius, terrainMask)) { noTerrainPosition = currentChunk.transform.Find("Down").position; //Down SpawnChunk(); } } else if (pm.moveDir.x > 0 && pm.moveDir.y > 0) { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right Up").position, checkerRadius, terrainMask)) { noTerrainPosition = currentChunk.transform.Find("Right Up").position; //Right up SpawnChunk(); } } else if (pm.moveDir.x > 0 && pm.moveDir.y < 0) { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right Down").position, checkerRadius, terrainMask)) { noTerrainPosition = currentChunk.transform.Find("Right Down").position; //Right down SpawnChunk(); } } else if (pm.moveDir.x < 0 && pm.moveDir.y > 0) { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left Up").position, checkerRadius, terrainMask)) { noTerrainPosition = currentChunk.transform.Find("Left Up").position; //Left up SpawnChunk(); } } else if (pm.moveDir.x < 0 && pm.moveDir.y < 0) { if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left Down").position, checkerRadius, terrainMask)) { noTerrainPosition = currentChunk.transform.Find("Left Down").position; //Left down SpawnChunk(); } } } void SpawnChunk() { int rand = Random.Range(0, terrainChunks.Count); latestChunk = Instantiate(terrainChunks[rand], noTerrainPosition, Quaternion.identity); spawnedChunks.Add(latestChunk); } void ChunkOptimzer() { optimizerCooldown -= Time.deltaTime; if (optimizerCooldown <= 0f) { optimizerCooldown = optimizerCooldownDur; //Check every 1 second to save cost, change this value to lower to check more times } 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); } } } }
July 23, 2024 at 8:57 am #15338Arthur EstevesParticipantJuly 23, 2024 at 4:05 pm #15343TerenceKeymaster::The
SpawnChunk()
does not seem to be firing. Let’s add the following lines into yourMapController
, then try moving around and see if any messages appear on your Console.void ChunkChecker() { if(!currentChunk) { return; } if (pm.moveDir.x > 0 && pm.moveDir.y == 0) { if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(20, 0, 0), checkerRadius, terrainMask)) { print("SpawnChunk right"); noTerrainPosition = player.transform.position + new Vector3(20, 0, 0); //Right SpawnChunk(); } } else if (pm.moveDir.x < 0 && pm.moveDir.y == 0) { if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(-20, 0, 0), checkerRadius, terrainMask)) { print("SpawnChunk left"); noTerrainPosition = player.transform.position + new Vector3(-20, 0, 0); //Left SpawnChunk(); } } else if (pm.moveDir.y > 0 && pm.moveDir.x == 0) { if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(0, 20, 0), checkerRadius, terrainMask)) { print("SpawnChunk up"); noTerrainPosition = player.transform.position + new Vector3(0, 20, 0); //Up SpawnChunk(); } } else if (pm.moveDir.y < 0 && pm.moveDir.x == 0) { if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(0, -20, 0), checkerRadius, terrainMask)) { print("SpawnChunk down"); noTerrainPosition = player.transform.position + new Vector3(0, -20, 0); //Down SpawnChunk(); } } else if (pm.moveDir.x > 0 && pm.moveDir.y > 0) { if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(20, 20, 0), checkerRadius, terrainMask)) { print("SpawnChunk up right"); noTerrainPosition = player.transform.position + new Vector3(20, 20, 0); //Right up SpawnChunk(); } } else if (pm.moveDir.x > 0 && pm.moveDir.y < 0) { if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(20, -20, 0), checkerRadius, terrainMask)) { print("SpawnChunk down right"); noTerrainPosition = player.transform.position + new Vector3(20, -20, 0); //Right down SpawnChunk(); } } else if (pm.moveDir.x < 0 && pm.moveDir.y > 0) { if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(-20, 20, 0), checkerRadius, terrainMask)) { print("SpawnChunk up left"); noTerrainPosition = player.transform.position + new Vector3(-20, 20, 0); //Left up SpawnChunk(); } } else if (pm.moveDir.x < 0 && pm.moveDir.y < 0) { if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(-20, -20, 0), checkerRadius, terrainMask)) { print("SpawnChunk down left"); noTerrainPosition = player.transform.position + new Vector3(-20, -20, 0); //Left down SpawnChunk(); } } }
Also, can you check if you have a trigger collider in each of your chunk prefabs?
July 27, 2024 at 8:08 am #15392Arthur EstevesParticipant::Hi Terance!
I put your lines on the script and trigger the collider on the chunks prefabs, but still don´t work
the script looked like this:
using System.Collections; using System.Collections.Generic; 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; PlayerMovement pm; [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() { pm = FindObjectOfType<PlayerMovement>(); } void Update() { ChunkChecker(); ChunkOptimzer(); } void ChunkChecker() { if(!currentChunk) { return; } if (pm.moveDir.x > 0 && pm.moveDir.y == 0) { if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(20, 0, 0), checkerRadius, terrainMask)) { print("SpawnChunk right"); noTerrainPosition = player.transform.position + new Vector3(20, 0, 0); //Right SpawnChunk(); } } else if (pm.moveDir.x < 0 && pm.moveDir.y == 0) { if(!Physics2D.OverlapCircle(player.transform.position + new Vector3(-20, 0, 0), checkerRadius, terrainMask)) { print("SpawnChunk left"); noTerrainPosition = player.transform.position + new Vector3(-20, 0, 0); //Left SpawnChunk(); } } else if (pm.moveDir.y > 0 && pm.moveDir.x == 0) { if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(0, 20, 0), checkerRadius, terrainMask)) { print("SpawnChunk up"); noTerrainPosition = player.transform.position + new Vector3(0, 20, 0); //Up SpawnChunk(); } } else if (pm.moveDir.y < 0 && pm.moveDir.x == 0) { if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(0, -20, 0), checkerRadius, terrainMask)) { print("SpawnChunk down"); noTerrainPosition = player.transform.position + new Vector3(0, -20, 0); //Down SpawnChunk(); } } else if (pm.moveDir.x > 0 && pm.moveDir.y > 0) { if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(20, 20, 0), checkerRadius, terrainMask)) { print("SpawnChunk up right"); noTerrainPosition = player.transform.position + new Vector3(20, 20, 0); //Right up SpawnChunk(); } } else if (pm.moveDir.x > 0 && pm.moveDir.y < 0) { if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(20, -20, 0), checkerRadius, terrainMask)) { print("SpawnChunk down right"); noTerrainPosition = player.transform.position + new Vector3(20, -20, 0); //Right down SpawnChunk(); } } else if (pm.moveDir.x < 0 && pm.moveDir.y > 0) { if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(-20, 20, 0), checkerRadius, terrainMask)) { print("SpawnChunk up left"); noTerrainPosition = player.transform.position + new Vector3(-20, 20, 0); //Left up SpawnChunk(); } } else if (pm.moveDir.x < 0 && pm.moveDir.y < 0) { if (!Physics2D.OverlapCircle(player.transform.position + new Vector3(-20, -20, 0), checkerRadius, terrainMask)) { print("SpawnChunk down left"); noTerrainPosition = player.transform.position + new Vector3(-20, -20, 0); //Left down SpawnChunk(); } } } void SpawnChunk() { int rand = Random.Range(0, terrainChunks.Count); latestChunk = Instantiate(terrainChunks[rand], noTerrainPosition, Quaternion.identity); spawnedChunks.Add(latestChunk); } void ChunkOptimzer() { optimizerCooldown -= Time.deltaTime; if (optimizerCooldown <= 0f) { optimizerCooldown = optimizerCooldownDur; //Check every 1 second to save cost, change this value to lower to check more times } 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); } } } }
Thanks for your attention
July 28, 2024 at 1:00 am #15398 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: