質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

1590閲覧

Unity 2Dでパズルを重ならないように置きたい

sumikko6210

総合スコア138

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2017/09/04 15:18

編集2017/09/05 03:09

イメージ説明

Unity2Dでドラッグ&ドロップではめていくパズルゲームを作成中です。
現在はめたときにパズルが重なってしまうので、パズルを重ならないようにしたいです。

Group.cs

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Group : MonoBehaviour 6{ 7 private Vector3 screenPoint; 8 private Vector3 offset; 9 10 bool isValidGridPos() 11 { 12 foreach (Transform child in transform) 13 { 14 Vector2 v = Grid.roundVec2(child.position); 15 16 if (!Grid.insideBorder(v)) 17 return false; 18 19 if (Grid.grid[(int)v.x, (int)v.y] != null && 20 Grid.grid[(int)v.x, (int)v.y].parent != transform) 21 return false; 22 } 23 return true; 24 } 25 26 void updateGrid() 27 { 28 for (int y = 0; y < Grid.h; ++y) 29 for (int x = 0; x < Grid.w; ++x) 30 if (Grid.grid[x, y] != null) 31 if (Grid.grid[x, y].parent == transform) 32 Grid.grid[x, y] = null; 33 34 foreach (Transform child in transform) 35 { 36 Vector2 v = Grid.roundVec2(child.position); 37 Grid.grid[(int)v.x, (int)v.y] = child; 38 } 39 } 40 41 void OnMouseDown() 42 { 43 // マウスカーソルは、スクリーン座標なので、 44 // 対象のオブジェクトもスクリーン座標に変換してから計算する。 45 46 // このオブジェクトの位置(transform.position)をスクリーン座標に変換。 47 screenPoint = Camera.main.WorldToScreenPoint(transform.position); 48 // ワールド座標上の、マウスカーソルと、対象の位置の差分。 49 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); 50 51 52 } 53 54 void OnMouseDrag() 55 { 56 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 57 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset; 58 transform.position = currentPosition; 59 } 60 61 void OnMouseUp() 62 { 63 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 64 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset; 65 66 67 if (isValidGridPos()) 68 { 69 transform.position = Grid.roundVec2(currentPosition); 70 71 Grid.deleteFullRows(); 72 73 FindObjectOfType<Spawner>().spawnNext(); 74 75 } 76 else 77 { 78 transform.position = GameObject.Find("Spawner").transform.position; 79 } 80 } 81 82 83 // Use this for initialization 84 void Start() 85 { 86 if (!isValidGridPos()) 87 { 88 Debug.Log("GAME OVER"); 89 } 90 } 91 92 // Update is called once per frame 93 void Update() 94 { 95 96 } 97} 98 99

Grid.cs

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Grid: MonoBehaviour { 6 public static int w = 10; 7 public static int h = 20; 8 public static Transform[,] grid = new Transform[w, h]; 9 10 public static Vector2 roundVec2(Vector2 v) 11 { 12 return new Vector2(Mathf.Round(v.x), 13 Mathf.Round(v.y)); 14 } 15 16 public static bool insideBorder(Vector2 pos) 17 { 18 return ((int)pos.x >= 0 && 19 (int)pos.x < w && 20 (int)pos.y >= 0); 21 } 22 23 public static void deleteRow(int y) 24 { 25 for(int x = 0; x < w; ++x) 26 { 27 Destroy(grid[x, y].gameObject); 28 grid[x, y] = null; 29 } 30 } 31 32 public static void deleteLine(int x) 33 { 34 for (int y = 0; y < h; ++x) 35 { 36 Destroy(grid[x, y].gameObject); 37 grid[x, y] = null; 38 } 39 } 40 41 public static bool isRowFull(int y) 42 { 43 for (int x = 0; x < w; ++x) 44 if (grid[x, y] == null) 45 return false; 46 return true; 47 } 48 49 public static void deleteFullRows() 50 { 51 for(int y = 0; y < h; ++y){ 52 if (isRowFull(y)) 53 { 54 deleteRow(y); 55 } 56 } 57 } 58}

Spawner.cs

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Spawner : MonoBehaviour 6{ 7 8 public GameObject[] groups; 9 public void spawnNext() 10 { 11 int i = Random.Range(0, groups.Length); 12 13 Instantiate(groups[i], 14 transform.position, 15 Quaternion.identity); 16 } 17 18 // Use this for initialization 19 void Start() 20 { 21 spawnNext(); 22 } 23 24 // Update is called once per frame 25 void Update() 26 { 27 28 } 29}

二次元配列でパズルの座標を決めているのでif(grid[x, y] == null)などでパズルがあるかどうか判別しようとしているのですが、うまくいきません。
ご教授よろしくお願いいたします。

環境 unity5.6.2f1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

void Update()
{
if (isValidGridPos())
{
// It's valid. Update grid.
updateGrid();
}
}
を入れておらず、更新していないだけでした。

投稿2017/09/05 04:19

sumikko6210

総合スコア138

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問