前のUnityの記事の続きは書かないのか?
ごめん
今回から初心者向けに2Dでゲームを制作していこうと思います。
何でいきなり3D からでなく2Dから始めるかというと、3Dでゲームを作るのは割と難しいからです。
最初はプレハブ制作からしていきます。
テトリスミノの形はO、I、S、Z、L、J、Tの形のものを作ります。

テトリスミノの四角は2Dのスプライトで表現します。
そして親オブジェクトにはこのコードをアタッチします。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mino : MonoBehaviour
{
// 前にminoが落ちたタイム
public float previousTime;
// minoが落ちるタイム
public float fallTime = 1f;
// mino回転
public Vector3 rotationPoint;
// ステージの大きさ
private static int width = 10;
private static int height = 20;
// ステージにあるminoを配置するマス目
private static Transform[,] grid = new Transform[width, height];
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
MinoMovememt();
}
private void MinoMovememt()
{
// 左矢印キーで左に動く
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.position += new Vector3(-1, 0, 0);
if (!ValidMovement())
{
transform.position -= new Vector3(-1, 0, 0);
}
}
// 右矢印キーで右に動く
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
transform.position += new Vector3(1, 0, 0);
if (!ValidMovement())
{
transform.position -= new Vector3(1, 0, 0);
}
}
// 自動で下に移動させつつ、下矢印キーでも移動する
else if (Input.GetKeyDown(KeyCode.DownArrow) || Time.time - previousTime >= fallTime)
{
transform.position += new Vector3(0, -1, 0);
if (!ValidMovement())
{
transform.position -= new Vector3(0, -1, 0);
AddToGrid();
CheckLines();
this.enabled = false;
//minoを新たに出す
FindObjectOfType<SpawnMino>().NewMino();
}
previousTime = Time.time;
}
// スペースキーで回転
else if (Input.GetKeyDown(KeyCode.Space))
{
transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0, 0, 1), 90);
if (!ValidMovement())
{
transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0, 0, 1), -90);
}
}
}
public void CheckLines()
{
for (int i = height - 1; i >= 0; i--)
{
if (HasLine(i))
{
DeleteLine(i);
RowDown(i);
}
}
}
bool HasLine(int i)
{
for (int j = 0; j < width; j++)
{
if (grid[j, i] == null)
return false;
}
//GameManagementを呼び出す
FindObjectOfType<GameManagement>().AddScore();
return true;
}
//1列消す
void DeleteLine(int i)
{
for (int j = 0; j < width; j++)
{
Destroy(grid[j, i].gameObject);
grid[j, i] = null;
}
}
//1行落とす
public void RowDown(int i)
{
for (int y = i; y < height; y++)
{
for (int j = 0; j < width; j++)
{
if (grid[j, y] != null)
{
grid[j, y - 1] = grid[j, y];
grid[j, y] = null;
grid[j, y - 1].transform.position -= new Vector3(0, 1, 0);
}
}
}
}
//minoをマス目に加える
void AddToGrid()
{
foreach (Transform children in transform)
{
int roundX = Mathf.RoundToInt(children.transform.position.x);
int roundY = Mathf.RoundToInt(children.transform.position.y);
grid[roundX, roundY] = children;
}
}
// minoの移動範囲の制御
bool ValidMovement()
{
foreach (Transform children in transform)
{
int roundX = Mathf.RoundToInt(children.transform.position.x);
int roundY = Mathf.RoundToInt(children.transform.position.y);
// minoがステージよりはみ出さないように制御
if (roundX < 0 || roundX >= width || roundY < 0 || roundY >= height)
{
return false;
}
if (grid[roundX, roundY] != null)
{
return false;
}
}
return true;
}
}
アタッチの例

プレハブを作っただけではこのコードが動くか実証できません。
後でテストすることになります。
コメント