Doodle Jump Game Platform Spawn Issue Unity2D
I am making a game with different assets but the same physics and gameplay as Doodle Jump. I have a problem with the spawning of the platforms, I have made a "Destroyer" is attached to the player and deletes the platforms it touches while spawning new ones. The problem is that the platforms that are respawned are so far away from the "initial" start platforms of the game that the player cant reach them even if they keep spawning. Sayed simply the platforms don't appear close to the player in Y-axis but just spawn everywhere they are not supposed to. I am a beginner at Unity2D and at c#, this game is for my final project any input Is appreciated.
Destroyer Code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Destroy : MonoBehaviour { public GameObject player; public GameObject platformPrefab; public GameObject springPrefab; private GameObject myPlat;
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.name.StartsWith("Platform")) //if current collision starts with "Platform" then run
{
if(Random.Range(1, 7)==1) //probability of spawning a spring platform
{
Destroy(collision.gameObject);
Instantiate(springPrefab, new Vector2(Random.Range(-5.5f, 5.5f), player.transform.position.y + (14 + Random.Range(0.2f, 1.0f))), Quaternion.identity);
}
else
{
collision.gameObject.transform.position = new Vector2(Random.Range(-5.5f, 5.5f), player.transform.position.y + (14 + Random.Range(0.2f, 1.0f)));
}
} else if(collision.gameObject.name.StartsWith("Spring"))
{
if(collision.gameObject.name.StartsWith("Platform")) //if current collision starts with "Spring" then run
{
if(Random.Range(1, 7)==1) //if low probability of spring just keep the spring
{
collision.gameObject.transform.position = new Vector2(Random.Range(-5.5f, 5.5f), player.transform.position.y + (14 + Random.Range(0.2f, 1.0f)));
}
else //any other number spawn in the normal platform
{
Destroy(collision.gameObject); //destroy current spring
Instantiate(platformPrefab, new Vector2(Random.Range(-5.5f, 5.5f), player.transform.position.y + (14 + Random.Range(0.2f, 1.0f))), Quaternion.identity);
}
}
}
}
}