Search Unity

Flappy Bird Pipes are not moving towards the bird.

Discussion in '2D' started by edy_sefu41, Mar 17, 2018.

  1. edy_sefu41

    edy_sefu41

    Joined:
    Mar 14, 2018
    Posts:
    14
    Hi, so I'm working on a Flappy Birds clone and I got everything set up, but my pipes are moving in a oposite direction and I got stuck, I have no idea where this might come from.

    Video with how it's looking: http://recordit.co/k06W17cmBl

    Pipes Inspector: https://imgur.com/a/R3i8K

    The script: (this is the Parallaxer one, ask me if you need another one)

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Parallaxer : MonoBehaviour
    {

    class PoolObject
    {
    public Transform transform;
    public bool inUse;
    public PoolObject(Transform t) { transform = t; }
    public void Use() { inUse = true; }
    public void Dispose() { inUse = false; }
    }

    [System.Serializable]
    public struct YSpawnRange
    {
    public float minY;
    public float maxY;
    }

    public GameObject Prefab;
    public int poolSize;
    public float shiftSpeed;
    public float spawnRate;

    public YSpawnRange ySpawnRange;
    public Vector3 defaultSpawnPos;
    public bool spawnImmediate;
    public Vector3 immediateSpawnPos;
    public Vector2 targetAspectRatio;

    float spawnTimer;
    PoolObject[] poolObjects;
    float targetAspect;
    GameManager game;

    void Awake()
    {
    Configure();
    }

    void Start()
    {
    game = GameManager.Instance;
    }

    void OnEnable()
    {
    GameManager.OnGameOverConfirmed += OnGameOverConfirmed;
    }

    void OnDisable()
    {
    GameManager.OnGameOverConfirmed -= OnGameOverConfirmed;
    }

    void OnGameOverConfirmed()
    {
    for (int i = 0; i < poolObjects.Length; i++)
    {
    poolObjects.Dispose();
    poolObjects.transform.position = Vector3.one * 1000;
    }
    Configure();
    }

    void Update()
    {
    if (game.GameOver) return;

    Shift();
    spawnTimer += Time.deltaTime;
    if (spawnTimer > spawnRate)
    {
    Spawn();
    spawnTimer = 0;
    }
    }

    void Configure()
    {
    //spawning pool objects
    targetAspect = targetAspectRatio.x / targetAspectRatio.y;
    poolObjects = new PoolObject[poolSize];
    for (int i = 0; i < poolObjects.Length; i++)
    {
    GameObject go = Instantiate(Prefab) as GameObject;
    Transform t = go.transform;
    t.SetParent(transform);
    t.position = Vector3.one * 1000;
    poolObjects = new PoolObject(t);
    }

    if (spawnImmediate)
    {
    SpawnImmediate();
    }
    }

    void Spawn()
    {
    //moving pool objects into place
    Transform t = GetPoolObject();
    if (t == null) return;
    Vector3 pos = Vector3.zero;
    pos.y = Random.Range(ySpawnRange.minY, ySpawnRange.maxY);
    pos.x = (defaultSpawnPos.x * Camera.main.aspect) / targetAspect;
    t.position = pos;
    }

    void SpawnImmediate()
    {
    Transform t = GetPoolObject();
    if (t == null) return;
    Vector3 pos = Vector3.zero;
    pos.y = Random.Range(ySpawnRange.minY, ySpawnRange.maxY);
    pos.x = (immediateSpawnPos.x * Camera.main.aspect) / targetAspect;
    t.position = pos;
    Spawn();
    }

    void Shift()
    {
    //loop through pool objects
    //moving them
    //discarding them as they go off screen
    for (int i = 0; i < poolObjects.Length; i++)
    {
    poolObjects.transform.position += Vector3.right * shiftSpeed * Time.deltaTime;
    CheckDisposeObject(poolObjects);
    }
    }

    void CheckDisposeObject(PoolObject poolObject)
    {
    //place objects off screen
    if (poolObject.transform.position.x < (-defaultSpawnPos.x * Camera.main.aspect) / targetAspect)
    {
    poolObject.Dispose();
    poolObject.transform.position = Vector3.one * 1000;
    }
    }

    Transform GetPoolObject()
    {
    //retrieving first available pool object
    for (int i = 0; i < poolObjects.Length; i++)
    {
    if (!poolObjects.inUse)
    {
    poolObjects.Use();
    return poolObjects.transform;
    }
    }
    return null;
    }

    }
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    You can try with shift speed -1.

    or use vector3.left (then the shift speed should stay 1).
    poolObjects.transform.position += Vector3.left * shiftSpeed * Time.deltaTime;
     
  3. edy_sefu41

    edy_sefu41

    Joined:
    Mar 14, 2018
    Posts:
    14
    -1 SHIFT SPEED WORKED!! YOU'RE A GENIUS!