종스크롤 2D 슈팅게임 배경 스크롤링

Unity3D 2023. 2. 5. 20:04
반응형

 

 

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

public class BgScroller : MonoBehaviour
{
    public float speed;
    public Transform[] sprites;
    private Queue<int> q = new Queue<int>();

    private void Awake()
    {
        for (int i = sprites.Length-1; i >= 0; --i)
            q.Enqueue(i);
    }

    void Update()
    {
        var curPos = this.transform.position;
        var nextPos = Vector3.down * this.speed * Time.deltaTime;
        this.transform.position = curPos + nextPos;

        if (this.sprites[q.Peek()].position.y <= -12)
        {
            
            //0 -> 2 -> 1
            //1 -> 0 -> 2
            //2 -> 1 -> 0
            
            this.sprites[q.Peek()].transform.localPosition = this.sprites[q.Last()].localPosition + Vector3.up * 12;
            
            q.Enqueue(this.q.Dequeue());
            
        }
    }
}
반응형
: