素人Unity練習帳
前回の続き
次に、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<EnemyMove> ().MyHitPoint;
otherDiffencePoint = other.gameObject.GetComponent<EnemyMove> ().MyDiffencePoint;
otherAttackPoint = other.gameObject.GetComponent<EnemyMove> ().MyAttackPoint;
otherHitpoint = otherHitpoint + otherDiffencePoint - MyAttackPoint;
other.gameObject.GetComponent<EnemyMove> ().MyHitPoint = otherHitpoint;
}
GameObject.DestroyObject (gameObject);
}
}
--------------------------------- playerBullet.cs --------
と設定する
Enemyオブジェクトを
-----EnemyMove.cs--------------------------------------
void Update () {}に
-------------------
//MyHitPointが0より少なくなったら消 える
if (MyHitPoint <= 0) {
enemyState = State.Died;
GameObject.DestroyObject (gameObject);//自分が消える
}
-------------------
を追記。
void OnCollisionEnter(Collision other){
}
とりあえず、使わないので中身を削除
次は、相手の攻撃の処理です。
自分の攻撃と同様に
---------enemyBullet.cs-----------------
using UnityEngine;
using System.Collections;
public class enemyBullet : MonoBehaviour {
private int MyAttackPoint=5;
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 == "Player") {
otherHitpoint = other.gameObject.GetComponent<PlayerController> ().MyHitPoint;
otherDiffencePoint = other.gameObject.GetComponent<PlayerController> ().MyDiffencePoint;
otherAttackPoint = other.gameObject.GetComponent<PlayerController> ().MyAttackPoint;
otherHitpoint = otherHitpoint + otherDiffencePoint - MyAttackPoint;
Debug.Log ("HP "+otherHitpoint+" my "+MyAttackPoint+" AP "+otherAttackPoint);
other.gameObject.GetComponent<PlayerController> ().MyHitPoint = otherHitpoint;
}
GameObject.DestroyObject (gameObject);
}
}
---------enemyBullet.cs-----------------
PlayerController.csのvoid Update(){}に
//MyHitPointが0より少なくなったら消 える
if (MyHitPoint <= 0) {
GameObject.DestroyObject (gameObject);//自分が消える
}
を追記する。
確認をすると、自分のHitPointがゼロになると、画面がおかしくなる
ので
今のところは、
GameObject.DestroyObject (gameObject);//自分が消える
の行をコメントアウトしておく
戻る 次へ
Reon Viewin