-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipeSpawnerScript.cs
More file actions
42 lines (35 loc) · 1.13 KB
/
PipeSpawnerScript.cs
File metadata and controls
42 lines (35 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using UnityEngine;
public class PipeSpawnScript : MonoBehaviour
{
public GameObject pipePrefab;
public float spawnRate = 2f;
private float timer = 0f;
public float gapSize = 3.5f; // Space between top and bottom pipes
public float minY = -1f; // Minimum center Y of the gap
public float maxY = 3f; // Maximum center Y of the gap
void Start()
{
timer = spawnRate;
}
void Update()
{
timer += Time.deltaTime;
if (timer >= spawnRate)
{
SpawnPipe();
timer = 0f;
}
}
void SpawnPipe()
{
float yCenter = Random.Range(minY, maxY);
Vector3 spawnPos = new Vector3(transform.position.x, yCenter, 0);
GameObject newPipe = Instantiate(pipePrefab, spawnPos, Quaternion.identity);
// Assign LogicScript to the PipeMiddleScript on the new pipe
PipeMiddleScript middleScript = newPipe.GetComponentInChildren<PipeMiddleScript>();
if (middleScript != null)
{
middleScript.logic = FindFirstObjectByType<LogicScript>();
}
}
}