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

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

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

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Q&A

解決済

1回答

1298閲覧

埋まっていて見えないブロックを非表示にして、掘り起こしたときに表示させたい(UNET)

ATOMGAMES

総合スコア12

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

0グッド

0クリップ

投稿2018/10/09 05:50

前提・実現したいこと

UNETでのお話です。

現在、FPSのオンラインマルチプレイヤーでブロックが設置可能、破壊可能なマップを作ろうとしています。そこで問題となってくるのが頂点の数で、大量のブロックがあると、多くなってしまいます。
できれば、たくさんのブロックを1つのメッシュに統一して、軽くしたいのですが、計算が複雑すぎてできません。
イメージ説明
そこで、埋まっていて見えないブロックを非表示にすることを思いつきました。白いブロックの周り6箇所(緑色の部分)すべてにブロックがあったら、その白いブロックのMeshRendererがオフになるように、白いブロックの周りの6箇所1つでもブロックがなければ表示するようにしたいです。

あるいは、親オブジェクトの白いブロックのスクリプトで子の緑のオブジェクトの衝突判定がわかる方法があればお願いします。

シングルプレイヤーだったら、白いブロックを親オブジェクト、6つの緑色のブロックを子オブジェクトとして、図のように配置し、その6つの緑色のブロックすべてにスクリプトをつけて、「もし、OnTriggerEnterで範囲内に入ったgameObjectのtagが"Block"ならば、白いオブジェクトのスクリプトを検索して、BlockDetectionStateというある変数の値を+1する」というのを書いて、白いブロックのスクリプトのほうには、「もしBlockDetectionStateという値が6になったら、自身のMeshRendererをオフにする、BlockDetectionStateという値が5以下だったら自身のMeshRendererをオンにする」でできるのですが。

発生している問題・エラーメッセージ

開始したとき、埋まっていて見えないブロックはちゃんと非表示になるが、ブロックを掘り起こしたときに、そのまま非表示になったままとなっている。ちゃんと表示させたい。
イメージ説明

該当のソースコード

###Inspector
Block(Script:InvisibleBlock)

  • GreenBlock1(Script:BlockSenser)
  • GreenBlock2(Script:BlockSenser)
  • GreenBlock3(Script:BlockSenser)
  • GreenBlock4(Script:BlockSenser)
  • GreenBlock5(Script:BlockSenser)
  • GreenBlock6(Script:BlockSenser)

BlockSenser

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class BlockSenser : MonoBehaviour { 6 7 public int SenserID; 8 public GameObject Block; 9 int Detected = 1; 10 int NoDetected = 0; 11 12 private void OnTriggerEnter(Collider other) 13 { 14 if (other.gameObject.tag == "Level" | other.gameObject.tag == "PlayerBlock") 15 { 16 TellBlockDetection(SenserID, Detected); 17 } 18 } 19 20 private void OnTriggerExit(Collider other) 21 { 22 if (other.gameObject.tag == "Level" | other.gameObject.tag == "PlayerBlock") 23 { 24 TellBlockDetection(SenserID, NoDetected); 25 } 26 } 27 28 29 void TellBlockDetection(int senserID, int DetectedState) 30 { 31 if (senserID == 1) 32 { 33 Block.GetComponent<InvisibleBlock>().Senser1Detect(DetectedState); 34 } 35 if (senserID == 2) 36 { 37 Block.GetComponent<InvisibleBlock>().Senser2Detect(DetectedState); 38 } 39 if (senserID == 3) 40 { 41 Block.GetComponent<InvisibleBlock>().Senser3Detect(DetectedState); 42 } 43 if (senserID == 4) 44 { 45 Block.GetComponent<InvisibleBlock>().Senser4Detect(DetectedState); 46 } 47 if (senserID == 5) 48 { 49 Block.GetComponent<InvisibleBlock>().Senser5Detect(DetectedState); 50 } 51 if (senserID == 6) 52 { 53 Block.GetComponent<InvisibleBlock>().Senser6Detect(DetectedState); 54 } 55 } 56 57 58} 59

InvisibleBlock

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.Networking; 5 6public class InvisibleBlock : NetworkBehaviour { 7 [SyncVar(hook = "RefrashSenser1")] public int Senser1DetectState = 0; 8 [SyncVar(hook = "RefrashSenser2")] public int Senser2DetectState = 0; 9 [SyncVar(hook = "RefrashSenser3")] public int Senser3DetectState = 0; 10 [SyncVar(hook = "RefrashSenser4")] public int Senser4DetectState = 0; 11 [SyncVar(hook = "RefrashSenser5")] public int Senser5DetectState = 0; 12 [SyncVar(hook = "RefrashSenser6")] public int Senser6DetectState = 0; 13 14 public void RefrashSenser1(int S1DState) 15 { 16 Senser1DetectState = S1DState; 17 CheckIfAllSenserDetect(); 18 } 19 public void RefrashSenser2(int S2DState) 20 { 21 Senser2DetectState = S2DState; 22 CheckIfAllSenserDetect(); 23 } 24 public void RefrashSenser3(int S3DState) 25 { 26 Senser3DetectState = S3DState; 27 CheckIfAllSenserDetect(); 28 } 29 public void RefrashSenser4(int S4DState) 30 { 31 Senser4DetectState = S4DState; 32 CheckIfAllSenserDetect(); 33 } 34 public void RefrashSenser5(int S5DState) 35 { 36 Senser5DetectState = S5DState; 37 CheckIfAllSenserDetect(); 38 } 39 public void RefrashSenser6(int S6DState) 40 { 41 Senser6DetectState = S6DState; 42 CheckIfAllSenserDetect(); 43 } 44 45 46 public void Senser1Detect(int detectedState) 47 { 48 Senser1DetectState = detectedState; 49 } 50 51 public void Senser2Detect(int detectedState) 52 { 53 Senser2DetectState = detectedState; 54 } 55 56 public void Senser3Detect(int detectedState) 57 { 58 Senser3DetectState = detectedState; 59 } 60 61 public void Senser4Detect(int detectedState) 62 { 63 Senser4DetectState = detectedState; 64 } 65 66 public void Senser5Detect(int detectedState) 67 { 68 Senser5DetectState = detectedState; 69 } 70 71 public void Senser6Detect(int detectedState) 72 { 73 Senser6DetectState = detectedState; 74 } 75 76 77 78 public void CheckIfAllSenserDetect() 79 { 80 if (Senser1DetectState == 1 && Senser2DetectState == 1 && Senser3DetectState == 1 && Senser4DetectState == 1 && Senser5DetectState == 1 && Senser6DetectState == 1) 81 { 82 CmdInvisibleThisBlock(); 83 } 84 else 85 { 86 CmdVisibleThisBlock(); 87 } 88 } 89 [Command] 90 void CmdInvisibleThisBlock() 91 { 92 GetComponent<MeshRenderer>().enabled = false; 93 } 94 95 [Command] 96 void CmdVisibleThisBlock() 97 { 98 GetComponent<MeshRenderer>().enabled = true; 99 } 100 101} 102

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

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

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

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

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

guest

回答1

0

自己解決

自己解決しました。
6つの方向に、空のオブジェクトを白いブロックの中心に配置し、Rayを飛ばして衝突判定を行うことで解決しました。

投稿2018/10/09 15:35

ATOMGAMES

総合スコア12

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問