From d45f95d3ab387895d8af5b7181313d4bc533b1f7 Mon Sep 17 00:00:00 2001 From: GamerClassN7 Date: Sat, 29 Jan 2022 20:30:18 +0100 Subject: [PATCH] ProceduralGenerationFix --- Assets/Scripts/PlayerController.cs | 28 ++++++++++------ Assets/Scripts/ProceduralGeneration.cs | 45 ++++++++++++++------------ 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 900c10f..4f9fbdd 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -63,12 +63,16 @@ public class PlayerController : MonoBehaviour currentSpeed += 0.0005f; } - if (Input.GetAxis("Vertical") > 0 && currentSpeed < maxSpeed) { - if (!canMove) { + if (Input.GetAxis("Vertical") > 0 && currentSpeed < maxSpeed) + { + if (!canMove) + { canMove = true; } currentSpeed += 0.01f; - } else if (Input.GetAxis("Vertical") < 0 && currentSpeed > minSpeed) { + } + else if (Input.GetAxis("Vertical") < 0 && currentSpeed > minSpeed) + { currentSpeed -= 0.01f; } @@ -174,7 +178,8 @@ public class PlayerController : MonoBehaviour Vector3 gDirection; PlatformManager platform = other.gameObject.GetComponent(); gDirection = -other.GetContact(0).normal; - if (platform != null) { + if (platform != null) + { switch (platform.type) { case PlatformManager.PlatformType.Push: @@ -191,9 +196,12 @@ public class PlayerController : MonoBehaviour modifier += platform.speed; break; case PlatformManager.PlatformType.SpeedDown: - if (modifier - platform.speed >= 0) { + if (modifier - platform.speed >= 0) + { modifier -= platform.speed; - } else { + } + else + { modifier = 0.0f; } break; @@ -201,10 +209,12 @@ public class PlayerController : MonoBehaviour gDirection = -other.GetContact(0).normal; break; } - } else { - gDirection = -other.GetContact(0).normal; } - platformForward = other.transform.forward; + else + { + gDirection = -transform.up; + } + platformForward = other.transform.forward; this.downDirection = gDirection; Physics.gravity = gDirection * 9.81f; } diff --git a/Assets/Scripts/ProceduralGeneration.cs b/Assets/Scripts/ProceduralGeneration.cs index 8f74fa7..2f29df0 100644 --- a/Assets/Scripts/ProceduralGeneration.cs +++ b/Assets/Scripts/ProceduralGeneration.cs @@ -10,13 +10,21 @@ public class ProceduralGeneration : MonoBehaviour public GameObject lastBlock; private GameObject lastBlockPrefab; private int spavnetobjectIndex = 0; + private int maximumNumberOfPlatformsAtScene = 100; + private float maximumDistanceOfPlatformFromPlayer = 20.0f; - // Start is called before the first frame update - void Start() + + + GameObject drawPlatform(GameObject lastObject, GameObject objToSpawn) { - lastBlockPrefab = this.gameObject.transform.GetChild(0).gameObject; - lastBlock = this.gameObject.transform.GetChild(0).gameObject; - this.spawnedLevelBlocks.Add(lastBlock); + MeshFilter meshfilter = lastObject.GetComponent(); + Bounds bounds = meshfilter.mesh.bounds; + + float scale = meshfilter.transform.localScale.x; + Bounds b = new Bounds(bounds.center * scale, bounds.size * scale); + + Vector3 nextBlockLocation = new Vector3(lastObject.transform.position.x, lastObject.transform.position.y, lastObject.transform.position.z + b.size.z + 1.0f); + } List spawnSpiralOfPlatforms(GameObject lastObject, GameObject objToSpawn) @@ -53,19 +61,23 @@ public class ProceduralGeneration : MonoBehaviour return levelBlocksSpawnTemp; } + // Start is called before the first frame update + void Start() + { + lastBlockPrefab = this.gameObject.transform.GetChild(0).gameObject; + lastBlock = this.gameObject.transform.GetChild(0).gameObject; + this.spawnedLevelBlocks.Add(lastBlock); + } + // Update is called once per frame void Update() { - int maxNumberOfBlock = 200; - Vector3 playerPosition = this.player.transform.position; - Debug.Log("Index" + 0); - for (var i = 0; i < this.spawnedLevelBlocks.Count; i++) { float distance = Vector3.Distance(this.spawnedLevelBlocks[i].transform.position, playerPosition); - if (distance > 10.0f && this.spawnedLevelBlocks.Count >= maxNumberOfBlock) + if (distance > this.maximumDistanceOfPlatformFromPlayer && this.spawnedLevelBlocks.Count >= this.maximumNumberOfPlatformsAtScene) { Destroy(this.spawnedLevelBlocks[i]); this.spawnedLevelBlocks.Remove(this.spawnedLevelBlocks[i]); @@ -78,7 +90,7 @@ public class ProceduralGeneration : MonoBehaviour } - if (this.spawnedLevelBlocks.Count <= maxNumberOfBlock) + if (this.spawnedLevelBlocks.Count <= this.maximumNumberOfPlatformsAtScene) { int blockToSpawn = Random.Range(0, levelBlocks.Count); @@ -97,15 +109,7 @@ public class ProceduralGeneration : MonoBehaviour if ((blockToSpawn > -1 && (blockToSpawn < (levelBlocks.Count - 1)))) { - MeshFilter meshfilter = this.lastBlock.GetComponent(); - Bounds bounds = meshfilter.mesh.bounds; - - float scale = meshfilter.transform.localScale.x; - Bounds b = new Bounds(bounds.center * scale, bounds.size * scale); - - blockObjToSpawn = levelBlocks[blockToSpawn]; - Vector3 nextBlockLocation = new Vector3(this.lastBlock.transform.position.x, this.lastBlock.transform.position.y, this.lastBlock.transform.position.z + b.size.z + 1.0f); - instantiatedGameObject = Instantiate(blockObjToSpawn, nextBlockLocation, (Quaternion.identity)); + instantiatedGameObject = this.drawPlatform(this.lastBlock, this.levelBlocks[blockToSpawn]); this.spawnedLevelBlocks.Add(instantiatedGameObject); } @@ -120,7 +124,6 @@ public class ProceduralGeneration : MonoBehaviour blockObjToSpawn = levelBlocks[0]; } - Debug.Log("Spawn" + blockToSpawn); this.lastBlock = instantiatedGameObject; this.lastBlockPrefab = blockObjToSpawn;