素人Unity練習帳
前回の続き
次に、球が、何かに当たったら消えるようにする
Project>Assets>PrefabsのPlayerBulletを選択
playerBullet.csというscriptを作り
void Update () {}の下に
void OnCollisionEnter(Collision col){
GameObject.DestroyObject (col.gameObject);//当たった相手が消える
GameObject.DestroyObject (gameObject);//自分が消える
}
を追記すると、
playeの球が触れたものと球自体が消えるようになります

EnemyとBulletにもTagを設定する
新しくTag名を設定するため、
どのオブジェクトでもいいので選択して
Inspector>Tag>AddTagをクリック
Tagsの+マークをクリック
NewTagを消してEnemy
同じように、Bulletも作る

Hierarchy>Enemyを選択して Inspector>TagをEnemy

PrefabのEnemyBulletとPlayerBulletのTagをBulletにする


playerBullet.csを、
当たったら、PlayerBulletオブジェクトだけ消えるようにする
void OnCollisionEnter(Collision col){
GameObject.DestroyObject (gameObject);//自分が消える
}

同様にEnemyBulletにenemyBullet.csを作り
void OnCollisionEnter(Collision col){
GameObject.DestroyObject (gameObject);//自分が消える
}
を追記します。
Enemyの球も、何かに当たると消えます
戻る 次へ

Reon Viewin