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

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

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

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

Unity

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

Q&A

解決済

1回答

2593閲覧

Unity 2Dでドラッグしているものを常に一番上のレイヤーにしたい

sumikko6210

総合スコア138

C#

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

Unity

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

0グッド

0クリップ

投稿2017/09/21 16:32

編集2017/09/22 01:50

###前提・実現したいこと
Unity 2Dで現在ドラッグ&ドロップではめていくパズルを作っており、ドラッグしているものを常に一番上のレイヤーにしたいです。
###発生している問題

現在、ドラッグしたパズルがドロップしたパズルの下に入ってしまっている状況です。

###該当のソースコード
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 private Character owner; //持ち主キャラ 11 private int spawnerID = -1; //スポーン元のスポーナー番号 12 13 private SpriteRenderer sprite; 14 15 //座標が範囲内であるか,Gridに値があるかどうかを判定 16 public bool isValidGridPos() 17 { 18 foreach (Transform child in transform) 19 { 20 Vector2 v = Grid.roundVec2(child.position); 21 22 if (!Grid.insideBorder(v)) 23 return false; 24 25 if (Grid.grid[(int)v.x, (int)v.y] != null && 26 Grid.grid[(int)v.x, (int)v.y].parent != transform) 27 return false; 28 } 29 return true; 30 } 31 32 void updateGrid() 33 { 34 for (int y = 0; y < Grid.h; ++y) 35 for (int x = 0; x < Grid.w; ++x) 36 if (Grid.grid[x, y] != null) 37 if (Grid.grid[x, y].parent == transform) 38 Grid.grid[x, y] = null; 39 40 foreach (Transform child in transform) 41 { 42 Vector2 v = Grid.roundVec2(child.position); 43 Grid.grid[(int)v.x, (int)v.y] = child; 44 } 45 } 46 47 //オブジェクトをクリックする 48 void OnMouseDown() 49 { 50 // マウスカーソルは、スクリーン座標なので、 51 // 対象のオブジェクトもスクリーン座標に変換してから計算する。 52 53 // このオブジェクトの位置(transform.position)をスクリーン座標に変換。 54 screenPoint = Camera.main.WorldToScreenPoint(transform.position); 55 // ワールド座標上の、マウスカーソルと、対象の位置の差分。 56 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); 57 58 //スケールを大きく 59 transform.localScale = new Vector3(1.0f, 1.0f, 1.0f); 60 61 62 63 } 64 65 //オブジェクトをドラッグする 66 void OnMouseDrag() 67 { 68 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 69 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset; 70 transform.position = currentPosition; 71 } 72 73 //オブジェクトをドロップする 74 void OnMouseUp() 75 { 76 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 77 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset; 78 79 // 消えた列数リセット ///////////////////////////////////////////////// 80 Count count = GameObject.Find("Text").GetComponent<Count>(); 81 count.ResetCount(); 82 83 //currentPositionが範囲内であるか,Gridに値があるかどうかを判定 84 if (isValidGridPos()) 85 { 86 // It's valid. Update grid. 87 updateGrid(); 88 89 transform.position = Grid.roundVec2(currentPosition); 90 91 //Order in Layerを-1にする 92 sprite = GetComponent<SpriteRenderer>(); 93 if (sprite) 94 { 95 sprite.sortingOrder = -1; 96 } 97 98 //攻撃情報 99 //ピースの数(子オブジェクトの数)を判定 100 int ObjCount = this.transform.childCount; 101 102 BattleManager bm = GameObject.FindGameObjectWithTag("BattleManager").GetComponent<BattleManager>(); 103 bm.SetAttackInfo(ObjCount, owner, 0, 0, 0, BattleManager.TYPE.SPRING, 0, 0, 0, 0, 0); 104 bm.BlockSet(); 105 106 Grid.deleteFullRows(); 107 Grid.deleteFullLines(); 108 109 //消えた列数をテキストに反映 110 count.SetText(); 111 112 //指定範囲内ではブロックの当たり判定を無くす 113 BoxCollider2D collider = GetComponent<BoxCollider2D>(); 114 collider.enabled = false; 115 116 //次のブロックを呼び出す 117 FindObjectOfType<SpawnerManager>().requestSpawn(spawnerID); 118 119 120 } 121 else 122 { 123 //Spawnerオブジェクトの座標を取る 124 foreach(GameObject go in GameObject.FindGameObjectsWithTag("Spawner")) 125 { 126 if (spawnerID == go.GetComponent<Spawner>().SpawnerID) 127 { 128 transform.position = go.transform.position; 129 transform.localScale = new Vector3(0.3f, 0.3f, 1.0f); 130 } 131 } 132 133 } 134 135 } 136 137 138 // Use this for initialization 139 void Start() 140 { 141 //count = FindObjectOfType<Count>(); 142 } 143 144 // Update is called once per frame 145 void Update() 146 { 147 148 } 149 150 //スポーなーIDを設定 151 public void SetSpawnerID(int id) 152 { 153 spawnerID = id; 154 } 155 156 //持ち主を設定 157 public void SetOwner(Character character) 158 { 159 owner = character; 160 } 161} 162 163

スポーンしたばかりのブロックのOrder in Layerは0です

ご回答よろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

自己解決

C#

1 Vector3 pos = transform.position; 2 transform.position = new Vector3(pos.x, pos.y, pos.z+1);

z軸を操作することで被らないようにできました。

投稿2017/09/22 02:08

sumikko6210

総合スコア138

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問