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

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

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

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

Unity

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

Q&A

解決済

1回答

1122閲覧

Unity 2Dでオブジェクトを指定範囲内にドラッグ&ドロップしたい

sumikko6210

総合スコア138

C#

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

Unity

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

0グッド

0クリップ

投稿2017/09/04 06:35

現在、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 // このオブジェクトの位置(transform.position)をスクリーン座標に変換。 44 screenPoint = Camera.main.WorldToScreenPoint(transform.position); 45 // ワールド座標上の、マウスカーソルと、対象の位置の差分。 46 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); 47 } 48 49 void OnMouseDrag() 50 { 51 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 52 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset; 53 transform.position = currentPosition; 54 } 55 56 // Use this for initialization 57 void Start() 58 { 59 if (!isValidGridPos()) 60 { 61 Debug.Log("GAME OVER"); 62 } 63 } 64 65 // Update is called once per frame 66 void Update() 67 { 68 if (isValidGridPos()) 69 { 70 updateGrid(); 71 OnMouseDown(); 72 OnMouseDrag(); 73 } 74 else 75 { 76 Grid.deleteFullRows(); 77 78 FindObjectOfType<Spawner>().spawnNext(); 79 80 enabled = false; 81 } 82 } 83} 84

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} 59

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} 30 31

ご教授よろしくお願いいたします。

環境 unity 5.6.2f1

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

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

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

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

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

guest

回答1

0

自己解決

onMouseUp関数をつかったら出来ました。

投稿2017/09/04 15:04

sumikko6210

総合スコア138

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問