Hello,
This is my first ever attempt at making a game.
I am following the tutorial here: Recorded Video Session: Making A Flappy Bird Style Game - Unity Learn in order to make a flappy bird-like clone game. The game is a vertical scrolling game where a rocketship flies through space avoiding asteroids in order to reach a destination planet.
I have adapted the tutorial to my game mostly without any issue, except last night when I was working on creating the asteroid prefabs to spawn at a random location, I am not able to get it to spawn correctly. Much like how the tutorial spawns columns using a prefab at a predetermined x coordinate and a random y coordinate range, I am instead trying to spawn my asteroids at a predetermined y coordinate and a random x coordinate range instead. Code shown at end of post.
My problem:
The asteroids are spawning all along the same y coordinate, even as the scene and camera are scrolling vertically. I have included some screenshots to illustrate what I am talking about. In my code, I am trying to spawn the asteroid at y = 10 and scroll along with my background. Thus, I have also attached the scrolling background script to my prefab asteroid object. However, as the scene progresses, the asteroids all continue to spawn at the same part of the scene (eventually below my rocketship instead of ahead). What’s weird is that if I inspect the location of the background and ship as the scene progresses, their coords not correct relative to each other. For example, in this screenshot scenario, the y position of the rocketship is at Y = -3.15 whereas the asteroid that “spawned at Y = 10” have a Y coordinate = -6ish, even though the asteroids are ahead of the rocketship in the scene. The rocket and the background coord appear to be correct relative to each other.
What is the cause of my problem? How can I make my asteroids always spawn in front of my rocketship (like the columns in front of the flappybird)? Is this due to layering issue or code?
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AsteroidPool : MonoBehaviour
{
// Pool size may need adjustment
public int asteroidPoolSize = 5;
public GameObject asteroidPrefab;
public float spawnRate = 4f;
public float asteroidMin = -1f;
public float asteroidMax = 3.5f;
private GameObject[] asteroids;
private Vector2 objectPoolPosition = new Vector2(-15f, -25f);
private float timeSinceLastSpawn;
private int currentAsteroid = 0;
// Start is called before the first frame update
void Start()
{
asteroids = new GameObject[asteroidPoolSize];
for (int i = 0; i < asteroidPoolSize; i++)
{
// Quaternion allows us to use the rotation of the prefab?
asteroids[i] = (GameObject)Instantiate(asteroidPrefab, objectPoolPosition, Quaternion.identity);
}
}
// Update is called once per frame
void Update()
{
timeSinceLastSpawn += Time.deltaTime;
if (GameControl.instance.gameOver == false && timeSinceLastSpawn >= spawnRate)
{
// If it has been a certain amount of time elapsed, then we want to spawn an asteroid at
// a random position
// Reset timer
timeSinceLastSpawn = 0;
// Generate a random x position for the asteroid to spawn
float spawnXPos = Random.Range(asteroidMin, asteroidMax);
float spawnYPos = 10f;
// Cycle through the GameObj array of asteroids and set that asteroid position
asteroids[currentAsteroid].transform.position = new Vector2(spawnXPos, spawnYPos);
currentAsteroid++;
if (currentAsteroid >= asteroidPoolSize)
{
currentAsteroid = 0;
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScrollingBackground : MonoBehaviour
{
private Rigidbody2D rigidbody2D;
// Start is called before the first frame update
void Start()
{
rigidbody2D = GetComponent<Rigidbody2D>();
rigidbody2D.velocity = new Vector2(0, GameControl.instance.scrollSpeed);
}
// Update is called once per frame
void Update()
{
if (GameControl.instance.gameOver == true)
{
rigidbody2D.velocity = Vector2.zero;
}
}
}
Scene much later in the scene. Note that the asteroids are still spawning, just clumped up at the bottom behind the rocket.