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 1 week, 5 days ago by
Terence.
-
AuthorPosts
-
November 20, 2023 at 1:20 am #12244
sad agent
ParticipantHello, 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 #12248Terence
KeymasterHi 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 #12250sad agent
ParticipantI think they are filled up correcty, and other ideas?
November 21, 2023 at 8:16 am #12252Terence
KeymasterTry 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 #12265sad agent
ParticipantWhere should this message print? Nothing happened when I added this.
November 21, 2023 at 11:14 pm #12266sad agent
ParticipantIf you mean this console, then there is nothing here
November 22, 2023 at 12:21 am #12267Terence
KeymasterIf 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 #12268sad agent
Participantit 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 #12269Terence
KeymasterWhat do you mean? Where did you change the coords? In the inspector?
November 22, 2023 at 2:17 am #12271sad agent
ParticipantNo 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 #12272sad agent
Participantits like new vector3(20, 20, 0) etc
November 22, 2023 at 5:46 am #12273Josh
Silver Supporter (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!
November 23, 2023 at 3:13 am #12279sad agent
ParticipantI 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 #12281Terence
KeymasterIf I’m not wrong, your “Right” has to be right at the edge of your chunk. It’s too far away.
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: