Hello i got this code that generates level parts randomly but i need to remove already appeared level parts from the game ,the ones that my character already past but i want to store them somewhere to be able to get back on my 2d level by recreating same parts.What would be best approach to this? Some guy told me about seed generation and other something about storing information in a list or something.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelGenerator : MonoBehaviour
{
private const float PlayerDistanceSpawnLevelPart = 50f;
[SerializeField] private Transform LevelPartStart;
[SerializeField] private List<Transform> LevelPartList;
public GameObject player;
private Vector3 lastEndPosition;
private void Awake()
{
lastEndPosition = LevelPartStart.Find("EndPosition").position;
int StartingSpawnLevelParts = 5;
for (int i = 0; i < StartingSpawnLevelParts; i++)
{
SpawnLevelPart();
}
}
void Update()
{
if (Vector3.Distance(player.transform.position, lastEndPosition) < PlayerDistanceSpawnLevelPart)
{
//spawn another level part
SpawnLevelPart();
}
}
private void SpawnLevelPart()
{
Transform chosenLevelPart = LevelPartList[Random.Range(0, LevelPartList.Count)];
Transform lastLevelPartTransform = SpawnLevelPart(chosenLevelPart,lastEndPosition);
lastEndPosition = lastLevelPartTransform.Find("EndPosition").position;
}
private Transform SpawnLevelPart(Transform levelPart,Vector3 spawnPossition)
{
Transform levelPartTransform = Instantiate(levelPart, spawnPossition, Quaternion.identity);
return levelPartTransform;
}
}
Aucun commentaire:
Enregistrer un commentaire