0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

無限スクロールする背景の作り方 [Unity2D]

Posted at

サンプル

roop-background.gif

背景オブジェクト が無限にスクロールしながらカメラに追従していきます。

実装方法

同じ画像が横にいくつか並んでいるような背景を用意して、
カメラがある程度移動したところで背景を横にずらします。

背景画像サンプル

red-white-background.png

コード

少し変な書き方かもしれないので、気になった方は手直しして使ってください。

色々と処理を載せたファイル


using UnityEngine;
namespace Object.Stage
{
    public class Background
    {
        Camera camera;
        float xMiddleWidth;
        SpriteRenderer sprite;
        public Background(
            SpriteRenderer sprite,
            Camera camera,
            // 連続している画像の数
            int magnifications
        )
        {
            this.camera = camera;
            this.xMiddleWidth = sprite.bounds.size.x / magnifications;
            this.sprite = sprite;
        }

        // カメラ位置と現在の背景画像位置をもとに、背景画像のX座標を更新する
        public void CheckCameraPositionAndUpdate()
        {
            float xPosition = sprite.transform.position.x;
            DirectionMap direction = CalculateDirectionToReplace(xPosition, xMiddleWidth);
            if (direction == DirectionMap.center)
                return;
            sprite.transform.position = new Vector3(
                CalculateTargetPositionX(xPosition, xMiddleWidth, direction),
                sprite.transform.position.y,
                sprite.transform.position.z
            );
        }

        // 移動先のX座標を計算する
        float CalculateTargetPositionX(float xPosition, float xMiddleWidth, DirectionMap direction)
        {
            return xPosition + ((int)direction * xMiddleWidth);
        }

        // 背景画像を動かす方向を計算する
        DirectionMap CalculateDirectionToReplace(float xPosition, float xMiddleWidth)
        {
            float xCameraPosition = camera.transform.position.x;
            if (xCameraPosition > CalculateTargetPositionX(xPosition, xMiddleWidth, DirectionMap.front))
                return DirectionMap.front;
            if (xCameraPosition < CalculateTargetPositionX(xPosition, xMiddleWidth, DirectionMap.back))
                return DirectionMap.back;
            return DirectionMap.center;
        }
    }
    enum DirectionMap : int
    {
        front = 1,
        back = -1,
        center = 0
    }
}

MonoBehaviour

このファイルを背景画像コンポーネントにアタッチする。

using UnityEngine;

public class BackGround : MonoBehaviour
{
    Object.Stage.BackGround backGround;

    private void Start()
    {
        backGround = new Object.Stage.BackGround(
            GetComponent<SpriteRenderer>(), // 背景画像
            Camera.main, // 基準となるカメラ
            4 // [赤白][赤白][赤白][赤白] と4回繰り返しているので4。画像の大きさと相談しながら調整する。
        );
    }

    private void LateUpdate()
    {
        // カメラ位置と現在の背景画像位置をもとに、背景画像のX座標を更新する
        backGround.CheckCameraPositionAndUpdate();
    }
}

最後に

なんか変なことやっていましたらご指摘くださいm(_ _)m

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?