画面サイズに合わせた壁を自動で作成【Unityメモ】

今回はUnity2Dゲームで壁を作成していきます。固定で作成、スクリプトで作成、画面に合わせて作成。の3つを見ていきます。

はじめに

Unityのバージョンは2021.3.3f1です。(あまりバージョンは関係ないですが)

下記で作成したように、オブジェクトにボックスコライダーをアタッチする方法もありますが、今回はエッジコライダーを利用していきます。

作成開始

一応、下記の続きですが、ゼロから作成でも問題ないです。壁として機能しているのを確認するため、下記の続きとして作成しています。

まずは「空のオブジェクト」を追加、名前をWallCreateとします。

エッジコライダー2Dとラインレンダラーをアタッチ。

下記の様に、サイズとX、Y座標を指定。周りを囲うように壁が作成されます。

コライダーにマテリアルをセット(跳ね返り:1)して、試しに動かしてみると下記の様な感じに。

スクリプトで作成

次にスクリプトでこの壁を作成してみます。先ほどセットしたサイズ、XY座標はリセットしておきます。

下記のスクリプトを作成。

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

public class WallCreate : MonoBehaviour
{
    [SerializeField] private EdgeCollider2D _edgeCollider;
    [SerializeField] private LineRenderer _lineRender;
    [SerializeField] private Vector3 _leftTop, _leftBottom, _rightTop, _rightBottom;

    private void Awake()
    {
        Vector2[] colliderPoints = new Vector2[5];
        colliderPoints[0] = new Vector2(_leftTop.x,_leftTop.y);
        colliderPoints[1] = new Vector2(_leftBottom.x,_leftBottom.y);
        colliderPoints[2] = new Vector2(_rightBottom.x,_rightBottom.y);
        colliderPoints[3] = new Vector2(_rightTop.x,_rightTop.y);
        colliderPoints[4] = new Vector2(_leftTop.x, _leftTop.y);
        _edgeCollider.points = colliderPoints;

        Vector3[] linePositions = new Vector3[] { _leftTop, _leftBottom, _rightBottom, _rightTop, _leftTop };
        _lineRender.positionCount = linePositions.Length;
        _lineRender.SetPositions(linePositions);

    }
}

WallCreateオブジェクトにスクリプトをアタッチして、パラメータをセット。

実行すると、同じような壁が作成されます。パラメータの値を変更すると壁の位置を簡単に変更することができます。

画面サイズに合わせて壁を作成

次に画面枠のサイズに合わせて、自動でサイズが変わる壁を作成してみます。

スクリプトを下記に変更。

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

public class WallCreate : MonoBehaviour
{
    [SerializeField] private EdgeCollider2D _edgeCollider;
    [SerializeField] private LineRenderer _lineRender;

    private void Awake()
    {
        LeftTop = Camera.main.ScreenToWorldPoint(new Vector3(0, Screen.height, 0));
        LeftBottom = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 0));
        RightBottom = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, 0, 0));
        RightTop = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0));
        LeftTop.z = LeftBottom.z = RightBottom.z = RightTop.z = 0;

        Vector2[] colliderPoints = new Vector2[5];
        colliderPoints[0] = new Vector2(LeftTop.x,LeftTop.y);
        colliderPoints[1] = new Vector2(LeftBottom.x,LeftBottom.y);
        colliderPoints[2] = new Vector2(RightBottom.x,RightBottom.y);
        colliderPoints[3] = new Vector2(RightTop.x,RightTop.y);
        colliderPoints[4] = new Vector2(LeftTop.x, LeftTop.y);
        _edgeCollider.points = colliderPoints;

        Vector3[] linePositions = new Vector3[] { LeftTop, LeftBottom, RightBottom, RightTop, LeftTop };
        _lineRender.positionCount = linePositions.Length;
        _lineRender.SetPositions(linePositions);

    }
}

画面サイズに沿って壁が作成されます。

タイトルとURLをコピーしました