ProceduralGenerationFix

This commit is contained in:
GamerClassN7 2022-01-29 20:30:18 +01:00
parent 4707a51a68
commit d45f95d3ab
2 changed files with 43 additions and 30 deletions

View File

@ -63,12 +63,16 @@ public class PlayerController : MonoBehaviour
currentSpeed += 0.0005f; currentSpeed += 0.0005f;
} }
if (Input.GetAxis("Vertical") > 0 && currentSpeed < maxSpeed) { if (Input.GetAxis("Vertical") > 0 && currentSpeed < maxSpeed)
if (!canMove) { {
if (!canMove)
{
canMove = true; canMove = true;
} }
currentSpeed += 0.01f; currentSpeed += 0.01f;
} else if (Input.GetAxis("Vertical") < 0 && currentSpeed > minSpeed) { }
else if (Input.GetAxis("Vertical") < 0 && currentSpeed > minSpeed)
{
currentSpeed -= 0.01f; currentSpeed -= 0.01f;
} }
@ -174,7 +178,8 @@ public class PlayerController : MonoBehaviour
Vector3 gDirection; Vector3 gDirection;
PlatformManager platform = other.gameObject.GetComponent<PlatformManager>(); PlatformManager platform = other.gameObject.GetComponent<PlatformManager>();
gDirection = -other.GetContact(0).normal; gDirection = -other.GetContact(0).normal;
if (platform != null) { if (platform != null)
{
switch (platform.type) switch (platform.type)
{ {
case PlatformManager.PlatformType.Push: case PlatformManager.PlatformType.Push:
@ -191,9 +196,12 @@ public class PlayerController : MonoBehaviour
modifier += platform.speed; modifier += platform.speed;
break; break;
case PlatformManager.PlatformType.SpeedDown: case PlatformManager.PlatformType.SpeedDown:
if (modifier - platform.speed >= 0) { if (modifier - platform.speed >= 0)
{
modifier -= platform.speed; modifier -= platform.speed;
} else { }
else
{
modifier = 0.0f; modifier = 0.0f;
} }
break; break;
@ -201,8 +209,10 @@ public class PlayerController : MonoBehaviour
gDirection = -other.GetContact(0).normal; gDirection = -other.GetContact(0).normal;
break; break;
} }
} else { }
gDirection = -other.GetContact(0).normal; else
{
gDirection = -transform.up;
} }
platformForward = other.transform.forward; platformForward = other.transform.forward;
this.downDirection = gDirection; this.downDirection = gDirection;

View File

@ -10,13 +10,21 @@ public class ProceduralGeneration : MonoBehaviour
public GameObject lastBlock; public GameObject lastBlock;
private GameObject lastBlockPrefab; private GameObject lastBlockPrefab;
private int spavnetobjectIndex = 0; 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; MeshFilter meshfilter = lastObject.GetComponent<MeshFilter>();
lastBlock = this.gameObject.transform.GetChild(0).gameObject; Bounds bounds = meshfilter.mesh.bounds;
this.spawnedLevelBlocks.Add(lastBlock);
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<GameObject> spawnSpiralOfPlatforms(GameObject lastObject, GameObject objToSpawn) List<GameObject> spawnSpiralOfPlatforms(GameObject lastObject, GameObject objToSpawn)
@ -53,19 +61,23 @@ public class ProceduralGeneration : MonoBehaviour
return levelBlocksSpawnTemp; 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 // Update is called once per frame
void Update() void Update()
{ {
int maxNumberOfBlock = 200;
Vector3 playerPosition = this.player.transform.position; Vector3 playerPosition = this.player.transform.position;
Debug.Log("Index" + 0);
for (var i = 0; i < this.spawnedLevelBlocks.Count; i++) for (var i = 0; i < this.spawnedLevelBlocks.Count; i++)
{ {
float distance = Vector3.Distance(this.spawnedLevelBlocks[i].transform.position, playerPosition); 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]); Destroy(this.spawnedLevelBlocks[i]);
this.spawnedLevelBlocks.Remove(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); int blockToSpawn = Random.Range(0, levelBlocks.Count);
@ -97,15 +109,7 @@ public class ProceduralGeneration : MonoBehaviour
if ((blockToSpawn > -1 && (blockToSpawn < (levelBlocks.Count - 1)))) if ((blockToSpawn > -1 && (blockToSpawn < (levelBlocks.Count - 1))))
{ {
MeshFilter meshfilter = this.lastBlock.GetComponent<MeshFilter>(); instantiatedGameObject = this.drawPlatform(this.lastBlock, this.levelBlocks[blockToSpawn]);
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));
this.spawnedLevelBlocks.Add(instantiatedGameObject); this.spawnedLevelBlocks.Add(instantiatedGameObject);
} }
@ -120,7 +124,6 @@ public class ProceduralGeneration : MonoBehaviour
blockObjToSpawn = levelBlocks[0]; blockObjToSpawn = levelBlocks[0];
} }
Debug.Log("Spawn" + blockToSpawn);
this.lastBlock = instantiatedGameObject; this.lastBlock = instantiatedGameObject;
this.lastBlockPrefab = blockObjToSpawn; this.lastBlockPrefab = blockObjToSpawn;