Forum begins after the advertisement:
[Part 2] Map Generation
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 2] Map Generation
- This topic has 20 replies, 3 voices, and was last updated 12 months ago by Terence.
-
AuthorPosts
-
November 20, 2023 at 1:20 am #12244::
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); } }
November 20, 2023 at 6:29 am #12245November 20, 2023 at 10:55 pm #12248::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.November 21, 2023 at 4:17 am #12250November 21, 2023 at 8:16 am #12252::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.
November 21, 2023 at 11:09 pm #12265November 21, 2023 at 11:14 pm #12266November 22, 2023 at 12:21 am #12267::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.
November 22, 2023 at 1:16 am #12268::it stopped working when i changed cords for
currentChunk.transform.Find("Right").position
inif(!Physics2D.OverlapCircle(currentChunk.transform.Find("Right").position, checkerRadius, terrainMask))
November 22, 2023 at 1:53 am #12269November 22, 2023 at 2:17 am #12271::No you were supposed to put actual cords in the code before instead of
currentChunk.transform.Find("Right").position
etcNovember 22, 2023 at 2:17 am #12272November 22, 2023 at 5:46 am #12273::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!
November 23, 2023 at 3:13 am #12279::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
November 23, 2023 at 11:28 am #12281 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: