前提・実現したいこと
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
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。