前提・実現したいこと
今、敵がキャラクターに向かって飛ばす弾の実装を行っています。空のオブジェクトにスクリプトをアタッチし、弾を飛ばす工程まではできたのですが、MeinCameraに写っていない時にもキャラに向かって弾が飛んでしまいます。ネットに載っている情報を参考に、試行錯誤したのですが、上手くいきませんでした。アドバイス等あれば、よろしくお願いします。
参考にしたサイト
・http://tsubakit1.hateblo.jp/entry/20131007/1381156232
発生している問題・エラーメッセージ
画面外から敵の弾が飛んできてしまう
エラーメッセージ 無し
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class enemyattack : MonoBehaviour 6{ 7 //プレイヤーオブジェクト 8 public GameObject player; 9 //弾のプレハブオブジェクト 10 public GameObject tama; 11 12 public int attackPoint = 10; 13 private LifeScript lifeScript; 14 15 16 private float targetTime = 2.0f; 17 private float currentTime = 0; 18 19 void Start() 20 { 21 lifeScript = GameObject.FindGameObjectWithTag("HP").GetComponent<LifeScript>(); 22 } 23 24 // Update is called once per frame 25 void Update() 26 { 27 28 currentTime += Time.deltaTime; 29 if (targetTime < currentTime) 30 { 31 currentTime = 0; 32 var pos = this.gameObject.transform.position; 33 var t = Instantiate(tama) as GameObject; 34 t.transform.position = pos; 35 Vector2 vec = player.transform.position - pos; 36 37 t.GetComponent<Rigidbody2D>().velocity = vec; 38 foreach (Transform n in transform) 39 { 40 Destroy(n.gameObject); 41 } 42 transform.DetachChildren(); 43 } 44 } 45 void OnCollisionEnter2D(Collision2D col) 46 { 47 if (col.gameObject.tag == "UnityChan") 48 { 49 50 lifeScript.LifeDown(attackPoint); 51 52 Destroy(gameObject); 53 } 54 55 if (col.gameObject.tag == "Field") 56 { 57 Destroy(gameObject); 58 } 59 60 if (col.gameObject.tag == "Enemy") 61 { 62 Destroy(gameObject); 63 } 64 65 } 66 67}
試したこと
カメラ外の処理を無効にする方法を調べた。
補足情報(FW/ツールのバージョンなど)
Unity 2018.2.11f1 (64-bit)
回答1件
あなたの回答
tips
プレビュー