Hi, I haven’t seen this question on Unity forums, nor have I found it by Googling it. What I’m making is a 2d Endless runner. I have my Spawner script and my Obstacle Movement script that both work great. But what I can’t figure out how to do is to prevent the Obstacles from overlapping when they’re spawned.
I have the Obstacles spawning at different speeds and different positions on the Y axis, which I like that way the game isn’t repetitive and boring. But one Obstacle will spawn in a certain position, then another one in the exact same position, but at a faster speed, then it overlaps the first obstacle. I would like to prevent that, but I’m not sure how. There are different Y positions where the Obstacles are being spawned from, and Im wondering if i can spawn one obstacle from that certain position, then if another obstacle spawns from that same Y position, it can only spawn when the first obstacle reaches a certain point as its moving to the left, on the X. Hopefully that makes sense.
I am new to coding and Unity, so I appreciate any help you all are willing to give me. Thank you, and here are my Spawner and Obstacle scripts:
public class Spawner : MonoBehaviour {
[SerializeField] GameObject[ ] obstacles;
[SerializeField] Transform obstaclesPos;
void Start()
{
//StartObstaclesMove();
}
public void StartObstaclesMove()
{
StartCoroutine(InstanteObstacles(0.5f));
}
IEnumerator InstanteObstacles ( float dt)
{
yield return new WaitForSeconds(dt);
if (GameManager.instance.bGameStarted)
{
Transform tt = obstaclesPos.GetChild(Random.Range(0, obstaclesPos.childCount));
int randomObstacle = Random.Range(0, obstacles.Length);
GameObject obs = GameObject.Instantiate(obstacles[randomObstacle]);
obs.transform.position = tt.position;
StartCoroutine(InstanteObstacles(dt));
}
}
}
And the Obstacle script:
public class ObstacleMovement : MonoBehaviour {
[SerializeField] float moveSpeed;
public bool isEnemy = false;
private void Awake()
{
moveSpeed = Random.Range(0.03f, 0.15f);
}
void Update()
{
Vector3 currentpos = transform.position;
currentpos.x -= moveSpeed; //* Time.deltaTime;
transform.position = currentpos;
if (transform.position.y < -15f)
{
Destroy(gameObject);
}
}
}
Here’s something i just tried from throwing together previous scripts, but I’m really lost. I’ve missed alot here:
[SerializeField] GameObject[] obstacles;
[SerializeField] Transform obstaclesPos;
[SerializeField] float minX;
[SerializeField] Transform currentObstacle;
void Start()
{
//StartObstaclesMove();
while (true)
{
if (!currentObstacle || currentObstacle.position.x < minX)
{
var prefab = Random.Range(0, 10) < 5 ???;
currentObstacle = Instantiate(obstacles, Vector2(10, -4), Quaternion.identity);
}
yield;
}
}
public void StartObstaclesMove()
{
StartCoroutine(InstanteObstacles(2f));
}
IEnumerator InstanteObstacles(float dt)
{
yield return new WaitForSeconds(dt);
if (GameManager.instance.bGameStarted)
{
Transform tt = obstaclesPos.GetChild(Random.Range(0, obstaclesPos.childCount));
int randomObstacle = Random.Range(0, obstacles.Length);
GameObject obs = GameObject.Instantiate(obstacles[randomObstacle]);
obs.transform.position = tt.position;
StartCoroutine(InstanteObstacles(dt));
}
}
}