球が当たったら、消える 2
次に、PlayerBulletがEnemyに当たったら、Enemyが消えるようにする
PlayerBulletオブジェクトのスクリプトを使ってもできるのですが
あとあとEnemyの体力などのパラメーター考えたときのために
Enemyオブジェクトの方に記述します
EnemyMove.csに
void OnCollisionEnter(Collision other){
if (other.gameObject.tag == "Bullet") {
GameObject.DestroyObject (gameObject);//自分が消える
}
}
を、追記する。PlayertBulletが当たるEnemyが消えるようになる。
単純当たると消えるだけだと、面白みもないので
初めの体力が、球が当たると減って最後に消えるというふうにする
PlayerとEnemyのオブジェクトに
public int MyHitPoint=20;
public int MyAttackPoint=1;
public int MyDiffencePoint =1;
PlayerBulletオブジェクトを
------playerBullet.cs-----------------------------------
using UnityEngine;
using System.Collections;
public class playerBullet : MonoBehaviour {
private int MyAttackPoint=3;
private int otherHitpoint;
private int otherDiffencePoint;
private int otherAttackPoint;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision other){
if (other.gameObject.tag == "Enemy") {
otherHitpoint = other.gameObject.GetComponent
().MyHitPoint;
otherDiffencePoint = other.gameObject.GetComponent ().MyDiffencePoint;
otherAttackPoint = other.gameObject.GetComponent ().MyAttackPoint;
otherHitpoint = otherHitpoint + otherDiffencePoint - MyAttackPoint;
other.gameObject.GetComponent ().MyHitPoint = otherHitpoint;
}
GameObject.DestroyObject (gameObject);
}
}
--------------------------------- playerBullet.cs --------
と設定する
- 関連記事
-
YouTubeで動画を定期的に出しています。ご視聴いただけると嬉しいです。
Reon Labo
- 2016/11/16(水) 18:00:00|
- Unity
-
| トラックバック:0
-
| コメント:0