본문 바로가기
게임프로젝트/쿠키런 모작

배경 무한반복 스크롤링(2) 수정

by pudding81 2024. 3. 29.

 

배경에 콜라이더를 넣었더니 플레이어와 충돌 현상이 일어남

배경에 콜라이더를 삭제하고

스크립트를 각각 부착하고 X축의 길이를 숫자로 넣어줌 

 

 

 

공통 이동 스크립트 

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

public class Scrolling : MonoBehaviour
{
    public float speed;



    void Update()
    {
       // if (!GameManager.instance.isGameOver) 게임오버가 아니면
        
            //게임오브젝트를 왼쪽으로 움직임
            this.transform.Translate(Vector3.left * speed * Time.deltaTime);
        



    }
}

 

무한 반복 스크립트 

 

벽 루프

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

public class BackgroundLoop : MonoBehaviour
{
    private float width=17.88f; //배경의 가로길이
    private void Awake()
    {
        //가로길이를 측정
       // BoxCollider2D boxCollider = GetComponent<BoxCollider2D>();
       // width = boxCollider.size.x;
        //Debug.Log(width);
    }


    void Update()
    {
        //현재위치가 width 이상 됐을때 위치 재배치

        if (transform.position.x <= -width)
        {
            RePosition();
        }

    }

    private void RePosition()
    {
        //위치를 재배치
        //현재위치에서 가로길이 2배만큼 오른쪽으로 이동

        Vector2 offset = new Vector2(this.width * 2, 0);

        this.transform.position = (Vector2)this.transform.position + offset;



    }
}

 

불꽃 루프 

 

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

public class FireBgLoop : MonoBehaviour
{
    private float width = 26.32f; //배경의 가로길이
    private void Awake()
    {
        //가로길이를 측정
        // BoxCollider2D boxCollider = GetComponent<BoxCollider2D>();
        // width = boxCollider.size.x;
        //Debug.Log(width);
    }


    void Update()
    {
        //현재위치가 width 이상 됐을때 위치 재배치

        if (transform.position.x <= -width)
        {
            RePosition();
        }

    }

    private void RePosition()
    {
        //위치를 재배치
        //현재위치에서 가로길이 2배만큼 오른쪽으로 이동

        Vector2 offset = new Vector2(this.width * 2, 0);

        this.transform.position = (Vector2)this.transform.position + offset;



    }
}