実現したいこと
unityにて2Dアクションを作っています。
「敵が死んだら3秒後に復活する」というコードを書きたいのですが、死ぬところまではできたんですけどその後の、3秒後に復活するところで、詰まっています。
何を書き加えればいいですか?
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class BombEnemy : MonoBehaviour 6{ 7 public AudioClip deathSound; // 死亡時の音声 8 9 public float breakForce = 10.0f; // 破壊に必要な力 10 11 Animator anim; 12 [SerializeField] Transform endPoint; 13 14 Vector3 startPos; // 開始位置 15 Vector3 endPos; // 終端位置 16 Vector3 destPos; // 次の目的地 17 18 float speed = 3f; // 移動速度 19 float rotateSpeed = 180f; // 回転速度 20 float rotateNum; // 方向転換時の回転量 21 22 public float explosionForce = 10.0f; // 爆発の力 23 public float explosionRadius = 5.0f; // 爆発の半径 24 public GameObject explosionPrefab; // 爆発のエフェクト 25 26 private Rigidbody rb; 27 private AudioSource audioSource; 28 29 void Start() 30 { 31 anim = GetComponent<Animator>(); 32 startPos = transform.position; 33 endPos = endPoint.position; 34 destPos = endPos; 35 rb = GetComponent<Rigidbody>(); 36 audioSource = GetComponent<AudioSource>(); 37 } 38 39 void Update() 40 { 41 if (GameManagerScript.status != GameManagerScript.GAME_STATUS.Play) 42 { 43 return; 44 } 45 46 // 端に到達した時の方向転換処理 47 if ((destPos - transform.position).magnitude < 0.1f) 48 { 49 // 回転途中の場合 50 if (rotateNum < 180) 51 { 52 anim.SetBool("walk", false); 53 transform.position = destPos; 54 55 float addNum = rotateSpeed * Time.deltaTime; 56 rotateNum += addNum; 57 transform.Rotate(0, addNum, 0); 58 return; 59 } 60 // 回転し切った場合 61 // 次の目的地の設定と回転量のリセット 62 else 63 { 64 destPos = destPos == startPos ? endPos : startPos; 65 rotateNum = 0; 66 } 67 } 68 69 // 次の目的地に向けて移動する 70 anim.SetBool("walk", true); 71 transform.LookAt(destPos); 72 transform.position += transform.forward * speed * Time.deltaTime; 73 } 74 75 void OnCollisionEnter(Collision other) 76 { 77 // 衝撃を受けたら爆発する 78 if (other.relativeVelocity.magnitude > 0.0f) 79 { 80 Explode(); 81 } 82 } 83 84 void Explode() 85 { 86 // 爆発エフェクトを生成 87 Instantiate(explosionPrefab, transform.position, Quaternion.identity); 88 89 // 衝撃を加える 90 Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius); 91 foreach (Collider nearbyObject in colliders) 92 { 93 Rigidbody rb = nearbyObject.GetComponent<Rigidbody>(); 94 if (rb != null) 95 { 96 rb.AddExplosionForce(explosionForce, transform.position, explosionRadius); 97 } 98 99 // 壊れるオブジェクトだった場合は破壊する 100 Breakwall wall = nearbyObject.GetComponent<Breakwall>(); 101 if (wall != null) 102 { 103 wall.Break(); 104 } 105 } 106 107 // 自分を破壊する 108 Destroy(gameObject); 109 110 // 死亡時のSEを再生 111 if (deathSound != null) 112 { 113 audioSource.PlayOneShot(deathSound); 114 } 115 } 116}
試したこと
敵が死んだ後に、Prefabから生成しようとしたんですけど中々上手くいきませんでした。
補足
この敵は爆弾敵で、衝撃を受けたら爆発して死ぬコードを書きました。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/07/21 00:25