使用エディター:Unity 2017.1.1f1 (64-bit)
環境:Windows10 Home
アクションゲームでマリオのスターよろしく、一定時間無敵になり
その後解除されるアイテムを作ろうとしています。
どうしたらよいのでしょうか。
行ったアプローチ
アイテム取得時のHPを記録
タイマーセット
HPを1万ぐらい(時間内に削り切れない程度)与え
時間を描写(あと*秒)
時間後記録したHPで上書き
結果 while文で処理落ち
mutekijikanメソッドのほうにif文で処理しようとする
結果:HPが増えない上一度に2ずつカウントが減り、残り1でストップしてしまう
例:
略 else if (other.gameObject.tag == "Star") { motonoHP = tankHP; starcount = 20; tankHP += 10000; InvokeRepeating ("mutekijikan", 0f, 1f); tankHP = motonoHP; tankLife.text = "HP:" + tankHP; } } 略 void mutekijikan(){ if (starcount > 0) { tankLife.text = "むてきあと" + starcount + "秒"; starcount -= 1; } ``` TankHealth.cs ``` using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class TankHealth : MonoBehaviour { public GameObject effectPrefab1; public GameObject effectPrefab2; public int tankHP; public Text tankLife; public GameObject LifeItem; private int motonoHP; public GameObject Star_get_effect; public GameObject Tank; private float starcount; void Start(){ tankLife.text = "HP:" + tankHP; } void OnTriggerEnter(Collider other){ if (other.gameObject.tag == "EnemyShell") { tankHP -= 1; tankLife.text = "HP:" + tankHP; Destroy (other.gameObject); if (tankHP > 0) { GameObject effect1 = (GameObject)Instantiate (effectPrefab1, transform.position, Quaternion.identity); Destroy (effect1, 1.0f); } else { GameObject effect2 = (GameObject)Instantiate (effectPrefab2, transform.position, Quaternion.identity); Destroy (effect2, 1.0f); this.gameObject.SetActive (false); Invoke ("GoToGameOver", 1.5f); } } else if (other.gameObject.tag == "Star") { motonoHP = tankHP; starcount = 20; tankHP += 10000; while (starcount > 0) { InvokeRepeating ("mutekijikan", 0f, 1f); } tankHP = motonoHP; tankLife.text = "HP:" + tankHP; } } public void AddHP(int amount){ tankHP += amount; if (tankHP > 10 && tankHP < 500) { tankHP = 10; } tankLife.text = "HP:" + tankHP; } void GoToGameOver(){ SceneManager.LoadScene("GameOver"); } void mutekijikan(){ tankLife.text = "むてきあと" + starcount + "秒"; starcount -= 1; } } ``` Starcontroller.cs ``` using System.Collections; using System.Collections.Generic; using UnityEngine; public class Starcontroller : MonoBehaviour { public GameObject Destroyeffect; public AudioClip get_audio; public GameObject Star_get_effect; public GameObject Tank_effect; void OnTriggerEnter(Collider other){ if (other.gameObject.tag == "Player") { Destroy (gameObject); Instantiate (Destroyeffect, transform.position, transform.rotation); AudioSource.PlayClipAtPoint (get_audio, Camera.main.transform.position); StartCoroutine ("destroy_effect"); } } IEnumerator destroy_effect() { yield return new WaitForSeconds (0.5f); Destroy (Destroyeffect); Destroy (get_audio); } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/15 09:59
2017/10/15 10:01