raycastを飛ばして
当たったオブジェクトの色を変え、2秒程度したら元の色に戻すという処理を実装したいのですが2秒とどめてまたもとに戻すという処理がどのように書けばいいかわかりません。
解決策などあるでしょうか?
C#
1public void OnColliderTouchedtouch(){ 2 3 4 if (Input.GetMouseButtonDown(0)){ 5 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 6 RaycastHit hit; 7 8 try{ 9 10 11 if (Physics.Raycast (ray, out hit, Mathf.Infinity, mask)) { 12 13 14 switch (hit.collider.name) { 15 16 case "actionButton0": 17 18 hit.collider.GetComponent<MeshRenderer> ().material.color = Color.yellow; 19 20 21 22 gamemadefaultMaterial[0] = hit.collider.gameObject ; 23 24 25 break; 26 27 case "actionButton1": 28 29 hit.collider.GetComponent<MeshRenderer> ().material.color = Color.blue; 30 gamemadefaultMaterial[1] = hit.collider.gameObject ; 31 break; 32 33 case "actionButton2": 34 35 hit.collider.GetComponent<MeshRenderer> ().material.color = Color.yellow; 36 gamemadefaultMaterial[2] = hit.collider.gameObject ; 37 38 break; 39 } 40 41 42 } 43 }catch(NullReferenceException e){ 44 45 } 46 47 }else { 48 49 try{ 50 51 gamemadefaultMaterial[0].GetComponent<MeshRenderer> ().material.color = Color.blue; 52 gamemadefaultMaterial[1].GetComponent<MeshRenderer> ().material.color = Color.blue; 53 gamemadefaultMaterial[2].GetComponent<MeshRenderer> ().material.color = Color.blue; 54 55 }catch(NullReferenceException ex){ 56 57 } 58 } 59 60 }
変更後
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using System; 5public class vbScript : MonoBehaviour{ 6 7 GameObject[] virtualButtonObject = new GameObject[3]; 8 GameObject[] _3Dtext = new GameObject[3]; 9 public TextMesh[] _3DtextInstance = new TextMesh[3]; 10 Renderer[] virtualButtonMaterial = new Renderer[3]; 11 int mask = 1 << 8; 12 public sample sampleClass; 13 GameObject[] gamemadefaultMaterial = new GameObject[3]; 14 public static int timeSchedulleCount =1 ; 15 int colorFlag ; 16 void Start () { 17 18 _3DtextInstance [0].text = "1"; 19 StartCoroutine ("colorWait"); 20 } 21 22 // Update is called once per frame 23 void Update () { 24 25 OnColliderTouchedtouch (); 26 27 } 28 29 public IEnumerator colorWait(){ 30 yield return new WaitForSeconds (2.0f); 31 try{ 32 switch (colorFlag){ 33 case 0: 34 35 gamemadefaultMaterial [0].GetComponent<MeshRenderer> ().material.color = Color.blue; 36 37 break; 38 case 1 : 39 40 gamemadefaultMaterial[1].GetComponent<MeshRenderer> ().material.color = Color.blue; 41 42 break; 43 44 case 2 : 45 gamemadefaultMaterial[2].GetComponent<MeshRenderer> ().material.color = Color.blue; 46 break ; 47 48 } 49 50 }catch(NullReferenceException ex){ 51 } 52 } 53 public void OnColliderTouchedtouch(){ 54 if (Input.GetMouseButtonDown(0)){ 55 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 56 RaycastHit hit; 57 58 try{ 59 60 if (Physics.Raycast (ray, out hit, Mathf.Infinity, mask)) { 61 62 63 switch (hit.collider.name) { 64 65 case "actionButton0": 66 67 hit.collider.GetComponent<MeshRenderer> ().material.color = Color.yellow; 68 69 if (timeSchedulleCount < 6) 70 timeSchedulleCount++; 71 72 _3DtextInstance [0].text = timeSchedulleCount + ""; 73 74 gamemadefaultMaterial[0] = hit.collider.gameObject ; 75 76 colorFlag =0 ; 77 78 colorWait() ; 79 80 break; 81 82 case "actionButton1": 83 84 hit.collider.GetComponent<MeshRenderer> ().material.color = Color.yellow ; 85 gamemadefaultMaterial[1] = hit.collider.gameObject ; 86 colorFlag =1 ; 87 88 colorWait(); 89 break; 90 91 case "actionButton2": 92 93 hit.collider.GetComponent<MeshRenderer> ().material.color = Color.yellow; 94 gamemadefaultMaterial[2] = hit.collider.gameObject ; 95 if (timeSchedulleCount > 1) 96 timeSchedulleCount--; 97 98 _3DtextInstance [0].text = timeSchedulleCount + ""; 99 100 colorFlag =2 ; 101 102 colorWait(); 103 break; 104 } 105 } 106 }catch(NullReferenceException e){ 107 108 } 109 110 }else { 111 112 try{ 113 114 }catch(NullReferenceException ex){ 115 116 } 117 } 118 119 } 120}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/07 03:51
2017/11/07 04:15
2017/11/07 05:08