Hello everyone,
I’m a novice in Unity and code but trying to learn them while following a tutorial (The Unity Tutorial For Complete Beginners by Game Maker’s Toolkit) where we are building a similar game to Flappy Bird.
I’ve basically followed the steps, trying to understand the code but something’s off. In the PipeSpawnScript something is off because all my pipes spawn inside one another.
I will paste the code below, maybe someone could give me some guidance.
Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PipeSpawnScript : MonoBehaviour
{
public GameObject pipe;
public float spawnRate = 2;
private float timer = 0;
public float heightOffset = 10;
// Start is called before the first frame update
void Start()
{
spawnPipe();
}
// Update is called once per frame
void Update()
{
if (timer < spawnRate)
{
timer = timer + Time.deltaTime;
}
else
{
spawnPipe();
timer = 0;
}
}
void spawnPipe()
{
float lowestPoint = transform.position.y - heightOffset;
float highestPoint = transform.position.y + heightOffset;
Instantiate(pipe, new Vector3(transform.position.x, Random.Range(lowestPoint, highestPoint), 0), transform.rotation);
}
}