For my initiation with Unity I chose the Tutorial Recorded Video Session: Making A Flappy Bird Style Game.
I followed all the steps but the final result was not the expected.
My problem is in step 9 (Recycling Obstacles With Object Pooling).
Video showing the problem:
script and config images:
column script:
using UnityEngine;
using System.Collections;
public class Column : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
if(other.GetComponent<Bee>() != null)
{
GameControl.instance.BirdScored();
}
}
}
column pool script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColumnPool : MonoBehaviour
{
public int columnPoolSize = 5;
public GameObject columnPrefab;
public float spawnRate = 3f;
public float columnMin = -1f;
public float columnMax = 3.5f;
private GameObject[] columns;
private Vector2 objectPoolPosition = new Vector2 (-15f, -25f);
private float timeSinceLastSpawned;
private float spawnXPosition = 10f;
private int currentColumn = 0;
// Start is called before the first frame update
void Start()
{
columns = new GameObject[columnPoolSize];
for (int i = 0; i < columnPoolSize; i++){
columns [i] = (GameObject)Instantiate (columnPrefab, objectPoolPosition, Quaternion.identity);
}
}
// Update is called once per frame
void Update()
{
timeSinceLastSpawned += Time.deltaTime;
if (GameControl.instance.gameOver == false && timeSinceLastSpawned >= spawnRate){
float spawnYPosition = Random.Range (columnMin, columnMax);
columns[currentColumn].transform.position = new Vector2 (spawnXPosition, spawnYPosition);
currentColumn++;
if (currentColumn >= columnPoolSize){
currentColumn = 0;
}
}
}
}
do you mean, that the obstacles aren’t moving ? Then show “scrolling object” script.
And I would say, use code tags here in the forum for the scripts.
Or maybe, if the moving is working, then you should reset to zero the “timeSinceLastSpawned” after the object was “spawned” in the ColumnPoolScript (somewhere in the if-statement)
ps. thank you, it is just better to read the scripts here instead to click the links
The columns are always displayed in front of the scene and do not enter the view zone. It seems to have more than one overlap.
scrolling object script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScrollingObject : MonoBehaviour
{
private Rigidbody2D rb2d;
// Use this for initialization
void Start ()
{
//Get and store a reference to the Rigidbody2D attached to this GameObject.
rb2d = GetComponent<Rigidbody2D>();
//Start the object moving.
rb2d.velocity = new Vector2 (GameControl.instance.scrollSpeed, 0);
}
void Update()
{
// If the game is over, stop scrolling.
if(GameControl.instance.gameOver == true)
{
rb2d.velocity = Vector2.zero;
}
}
}
this float at one time will be greater as the spawn time: timeSinceLastSpawned += Time.deltaTime;
then the statement: if (GameControl.instance.gameOver == false && timeSinceLastSpawned >= spawnRate) will be executed every frame
You are right i forgot to reset timeSinceLastSpawned.
Thank you very much.
It behaves strangely when the columns are being placed. Can you watch the video? I don’t know if it’s normal but it looks like it has several columns overlapping.
you can pause the game and check where the columns are placed in the scene (select them in the hierarchy). Maybe it is just looking bad in the editor. Try to build the game and run it. Or change sprite filtering. Sorry, i am not sure about the graphics