Hello,
I’m trying to make a game where you need to climb up and after you passed 100 platform the sprite of platforms will change, so i made a script that spawn that platforms but the main problem is that no matter what i’m trying to do my code turns into long code that hard to read and costs a lot.
I really will appreciate every help :), even just if you could give me some source that could help me because i really tried to find any information but i didn’t found anything that could help me in my case.
Also, the main reason that I stacked is because I’m using object pooling and I don’t know how to change sprite after reach some height or count of platforms.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelManager : MonoBehaviour {
private float targetPlatformHeight = 2.8f;
#region Platforms
private int PlatformsToInit = 10;
private int platformsCounter = 0;
private int passedScorePlatforms = 0;
private int targetPlatformsCount = 9;
private int toNextPlatformCount = 0;
private float distanceToReplacePlatform = 10f;
private float platformDistance = 2f;
private float currentPlatformY = 2.8f;
#endregion
#region Walls
private int WallsToInit = 3;
private float distanceToReplaceWalls = 15f;
private float wallDistance = 9f;
private float currentWallY = 3.5f;
#endregion
private List<Transform> PlatformsToPoll = new List<Transform>();
private List<Transform> WallsToPoll = new List<Transform>();
Transform Platform;
Transform Walls;
private void Awake() {
Platform = Resources.Load<Transform>("pfPlatform");
Walls = Resources.Load<Transform>("pfWalls");
InitPlatforms();
InitWalls();
}
private void Start() {
}
private void Update() {
SpawnPlatforms();
SpawnWalls();
HandleHeightScore();
Debug.Log($"{platformsCounter}, {passedScorePlatforms}, {toNextPlatformCount}");
}
private void InitPlatforms() {
for (int i=0; i < PlatformsToInit; i++) {
if(platformsCounter < targetPlatformsCount) {
Vector2 pos = new Vector2(Random.Range(-5f, 5f), currentPlatformY);
Transform platform = Instantiate(Platform, pos, Quaternion.identity, transform);
currentPlatformY += platformDistance;
platformsCounter++;
toNextPlatformCount++;
PlatformsToPoll.Add(platform);
} else if (platformsCounter == targetPlatformsCount) {
Vector2 pos = new Vector2(Random.Range(-5f, 5f), currentPlatformY);
Transform platform = Instantiate(Platform, pos, Quaternion.identity, transform);
currentPlatformY += platformDistance;
platformsCounter = 0;
passedScorePlatforms++;
toNextPlatformCount++;
PlatformsToPoll.Add(platform);
}
}
}
public Sprite[] sprites;
private int index = 0;
private void SpawnPlatforms() {
for (int i = 0; i < PlatformsToPoll.Count; i++) {
if (PlayerController.instance.GetPlayerPosition().y - PlatformsToPoll[i].position.y >= distanceToReplacePlatform) {
Vector2 pos = new Vector2(Random.Range(-5f, 5f), currentPlatformY);
PlatformsToPoll[i].position = pos;
PlatformsToPoll[i].localScale = new Vector3(4f, 3.455f);
currentPlatformY += platformDistance;
platformsCounter++;
toNextPlatformCount++;
}
}
}
//private void SpawnPlatforms() {
// for (int i = 0; i < PlatformsToPoll.Count; i++) {
// if (PlayerController.instance.GetPlayerPosition().y - PlatformsToPoll[i].position.y >= distanceToReplacePlatform) {
// if (platformsCounter < targetPlatformsCount) {
// Vector2 pos = new Vector2(Random.Range(-5f, 5f), currentPlatformY);
// PlatformsToPoll[i].position = pos;
// PlatformsToPoll[i].localScale = new Vector3(4f, 3.455f);
// currentPlatformY += platformDistance;
// platformsCounter++;
// toNextPlatformCount++;
// }
// else if (platformsCounter == targetPlatformsCount) {
// if (passedScorePlatforms < targetPlatformsCount) {
// Vector2 pos = new Vector2(Random.Range(-5f, 5f), currentPlatformY);
// PlatformsToPoll[i].position = pos;
// PlatformsToPoll[i].localScale = new Vector3(4f, 3.455f);
// currentPlatformY += platformDistance;
// platformsCounter = 0;
// passedScorePlatforms++;
// toNextPlatformCount++;
// }
// else if (passedScorePlatforms == targetPlatformsCount) {
// Vector2 pos = new Vector2(0, currentPlatformY);
// PlatformsToPoll[i].position = pos;
// PlatformsToPoll[i].localScale = new Vector3(15f, 3.455f);
// currentPlatformY += platformDistance;
// platformsCounter = 0;
// passedScorePlatforms = 0;
// }
// }
// }
// }
//}
private void InitWalls() {
for (int i = 0; i < WallsToInit; i++) {
Vector2 pos = new Vector2(0, currentWallY);
Transform walls = Instantiate(Walls, pos, Quaternion.identity, transform);
currentWallY += wallDistance;
WallsToPoll.Add(walls);
}
}
private void SpawnWalls() {
for (int i = 0; i < WallsToPoll.Count; i++) {
if (PlayerController.instance.GetPlayerPosition().y - WallsToPoll[i].position.y > distanceToReplaceWalls) {
Vector2 pos = new Vector2(0, currentWallY);
WallsToPoll[i].position = pos;
currentWallY += wallDistance;
}
}
}
private void HandleHeightScore() {
if (PlayerController.instance.GetPlayerPosition().y >= targetPlatformHeight) {
Gamemanager.Instance.AddScore(1f);
targetPlatformHeight += 2f;
}
}
}
Thanks in advanced