Reputation: 41
I need help, I have a code for generating a level, each time the generation happens randomly, so I can't make it so that when the trigger is reached on a certain section of the level, at the end of the level on the last platform, more platforms were generated, I tried to search for an object through pos = GameObject.Find("Platform 4 End Platform(Clone)").transform.position;
, but in the end I did not come to success,can someone explain how to do it best? All the code is attached below, as I have a generation, etc.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class LevelGeneratorRock : MonoBehaviour
{
[SerializeField]
private GameObject startPlatform, endPlatform, platformPrefab, triggerPlatform;
private float blockWidth = 0.5f, blockHeight = 0.2f;
[SerializeField]
private int amountToSpawn = 100;
private int beginAmount = 0;
public Vector3 pos;
private List<GameObject> spawnedPlatforms = new List<GameObject>();
[SerializeField]
private GameObject playerPrefab;
void Awake()
{
beginAmount = 0;
amountToSpawn = 10;
InstantiateLevel();
}
const int MAX_PLATFORMS = 100;
void InstantiateLevel()
{
// Notice the conditional changed here
for (int i = beginAmount; i < beginAmount + amountToSpawn; i++)
{
GameObject newPlatform;
if (i == 0)
{
newPlatform = Instantiate(startPlatform);
}
else if (i == MAX_PLATFORMS)
{ // End Platform
newPlatform = Instantiate(endPlatform);
newPlatform.tag = "EndPlatform";
}
else if (i % 10 == 0)
{
newPlatform = Instantiate(triggerPlatform);
newPlatform.tag = "triggerPlatform";
}
else
{ // Normal platform
newPlatform = Instantiate(platformPrefab);
}
spawnedPlatforms.Add(newPlatform);
int left = Random.Range(0, 2);
if (left == 0)
{
newPlatform.transform.position =
new Vector3(pos.x - blockWidth, pos.y + blockHeight, pos.z);
}
else
{
newPlatform.transform.position =
new Vector3(pos.x, pos.y + blockHeight, pos.z + blockWidth);
}
pos = newPlatform.transform.position;
// fancy animation
if (i < 25)
{
float pos = newPlatform.transform.position.y;
newPlatform.transform.position =
new Vector3(newPlatform.transform.position.x,
newPlatform.transform.position.y - blockHeight * 3f,
newPlatform.transform.position.z);
newPlatform.transform.DOLocalMoveY(pos, 0.3f).SetDelay(i * 0.1f);
}
} // for loop
} // instantiate level
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("triggerPlatform"))
{
// Tell the level generator to spawn more platforms
{
beginAmount = beginAmount + 11;
amountToSpawn = 10;
InstantiateLevel();
}
}
}
}// class
I will also attach a photo from the editor
Upvotes: 3
Views: 160
Reputation: 64
It looks like you have all the platforms in the List spawnedPlatforms. You're already setting the tag too, so you could loop over the list and check the tag for "EndPlatform" right? If you're after a specific EndPlatform,
var platformToFind = "3";
foreach (var platform in spawnedPlatforms) {
if (platform.tag == "EndPlatform" && platform.name.Contains(platformToFind)) {
// Do something
}
}
Based on your comment, it sounds like you wish that every time the player jumps up 10 platforms, you want to create 10 more.
What this means is you need a trigger platform every 10 platforms. So you should change if (i == amountToSpawn - 90)
to if (i % 10 == 0)
. This is called the modulo operator, it gives you the remainder of a division. E.g. 13 % 10 = 3, or for our case: 10 % 0 = 0, 20 % 0 = 0, 30 % 0 = 0, etc.
It would look something like this:
void Awake()
{
beginAmount = 0;
amountToSpawn = 10;
InstantiateLevel();
}
const int MAX_PLATFORMS = 100;
void InstantiateLevel()
{
// Notice the conditional changed here
for (int i = beginAmount; i < beginAmount + amountToSpawn; i++) {
GameObject newPlatform;
if (i == 0) {
newPlatform = Instantiate(startPlatform);
} else if (i == MAX_PLATFORMS) { // End Platform
} else if (i % 10 == 0) {
newPlatform = Instantiate(triggerPlatform);
newPlatform.tag = "triggerPlatform";
} else { // Normal platform
}
}
Then in your player script, you'd have something like:
void OnCollisionEnter(Collision other) {
if (other.gameObject.tag.CompareTag("triggerPlatform")) {
// Tell the level generator to spawn more platforms
levelGenerator.beginAmount = levelGenerator.beginAmount + 11;
levelGenerator.amountToSpawn = 10;
levelGenerator.InstantiateLevel();
}
}
Upvotes: 1