1

当我有多个敌人时,他们有一个健康的浮点数250.0f。因此,当我向其中一个射击时,每次向敌人射击时,这个数字应该减少 50。但这是我的问题:第一个敌人在大约 4/5 次射击时死亡,但是当我开始向另一个(在第一个敌人之后)射击时,它需要 1 次射击并死亡。

所以这是玩家 Bullet 的 Bullet 脚本,称为 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;
        }

    }


}

}

我试图找出是否有人有问题或者是否有解决方法,但我在这里或谷歌上找不到解决方案。所以有人可以告诉我如果我做错了什么或者有教程什么的。

4

1 回答 1

6

中的属性Enemy是 all static,这意味着它们中的每一个只有一个在整个应用程序之间共享。所以下一个直接死去的敌人,将使用前一个已经死去的敌人的生命。

从,和属性中删除static修饰符。currEnemyLifecurrEnemyBulletscurrEnemyFuel

然后您需要更改在 中修改这些值的方式Bullet,因为您无法再通过EnemyAI. 我对 Unity3D 了解不多,但是在阅读了一些内容之后,我认为您需要执行以下操作:

var enemy = otherObject.GetComponent<EnemyClass>();
enemy.currEnemyLife -= 50f;
// etc...
于 2013-05-31T14:48:53.730 回答