當我有多個敵人時,他們的生命週期數爲250.0f
。所以當我向他們中的一個射擊時,每次向敵人射擊時這個數字應該減少50。但是我的問題是:第一個敵人死亡的時候大約有4/5次射擊,但是當我開始向另一個敵人射擊時(第一個敵人之後),它需要1次射擊並死亡。在Unity中四次射擊多個敵人時的問題
因此,這裏是爲玩家子彈的子彈腳本,叫Bullet.cs:
public class Bullet : MonoBehaviour {
public GameObject BulletObject;
public GameObject GlobelObjectExplosion;
public GameObject GlobelBulletExplosion;
public float life = 3.0f;
private EnemyAI enemy;
private Transform bulletTransform;
// Use this for initialization
void Start() {
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Enemy")
{
EnemyAI.currEnemyLife -= 50;
Player.score += 50;
//Creates an explosion on collision
GameObject objBullet = Instantiate(GlobelBulletExplosion, transform.position, Quaternion.identity) as GameObject;
Destroy(objBullet, 2.0f);
Debug.Log("ColliderWorking");
if(EnemyAI.currEnemyLife <= 0.0f)
{
Destroy(other.gameObject);
//creates an explosion when enemy is destoryed
GameObject obj = Instantiate(GlobelObjectExplosion, other.gameObject.transform.position, Quaternion.identity) as GameObject;
Destroy(obj, 2.0f);
Player.score += 250;
}
Destroy(gameObject);
}
}
// Update is called once per frame
void Update() {
life -= Time.deltaTime;
transform.Translate(0, 0, Player.bulletSpeed * Time.deltaTime);
if(life <= 0.0)
{
Destroy(gameObject);
}
}
}
而且繼承人的EnemyAI腳本,叫EnemyAI.cs:
public class EnemyAI: MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public GameObject explosion;
public static float maxEnemyLife = 250.0f;
public static float currEnemyLife;
public static float maxEnemyBullets = 60.0f;
public static float currEnemyBullets;
public static float maxEnemyFuel = 1260.0f;
public static float currEnemyFuel;
private Transform myTranform;
public static Transform LocalTransform;
void Awake(){
myTranform = transform;
LocalTransform = myTranform;
}
// Use this for initialization
void Start() {
if(GameObject.FindGameObjectWithTag("Player") != null){
GameObject go = GameObject.FindGameObjectWithTag("Player");
currEnemyFuel = maxEnemyFuel;
currEnemyLife = maxEnemyLife;
currEnemyBullets = maxEnemyBullets;
target = go.transform;
}
}
// Update is called once per frame
void Update() {
if (currEnemyFuel > maxEnemyFuel)
{
currEnemyFuel = maxEnemyFuel;
}
//Debug.DrawLine(target.position, myTranform.position, Color.cyan);
if(currEnemyFuel <= 0.0)
{
currEnemyFuel = 0.0f;
//myTranform.rotation = Vector3.zero;
//myTranform.position = Vector3.zero;
moveSpeed = 0;
}
else if (currEnemyFuel > 0.0)
{
//Look at target
if(GameObject.FindGameObjectWithTag("Player") != null){
myTranform.rotation = Quaternion.Slerp(myTranform.rotation, Quaternion.LookRotation(target.position - myTranform.position), rotationSpeed * Time.deltaTime);
//Move towards Target
myTranform.position += myTranform.forward * moveSpeed * Time.deltaTime;
currEnemyFuel -= 0.2f;
}
}
}
}
我試圖找出是否有人有一些問題,或者如果這是一種解決方法,但我可以在這裏找不到解決方案或谷歌。所以有人可以告訴我,如果我做錯了什麼或有教程或什麼的。
我沒有統一的專家,但我懷疑的是,'currEnemyLife'值是屏幕上的所有敵人都減少了。每個「Enemy」對象都需要包含自己特定的'currEnemyLife'值。 –