在Unity中围绕4次射击多个敌人的问题

Ash*_*h32 1 c# unity-game-engine

当我有多个敌人时,他们有一个漂浮的健康号码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);

    }

}
Run Code Online (Sandbox Code Playgroud)

}

继承人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;
        }

    }


}
Run Code Online (Sandbox Code Playgroud)

}

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

gre*_*g84 6

所有属性Enemy都是static,这意味着在整个应用程序之间只共享一个属性.因此,直接死亡的下一个敌人将使用前一个已经死亡的生命.

取出static从修改currEnemyLife,currEnemyBulletscurrEnemyFuel性能.

然后,您将需要更改修改这些值的方式Bullet,因为您无法再直接访问它们EnemyAI.我对Unity3D了解不多,但在做了一点阅读后我觉得你需要做这样的事情:

var enemy = otherObject.GetComponent<EnemyClass>();
enemy.currEnemyLife -= 50f;
// etc...
Run Code Online (Sandbox Code Playgroud)