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

时间:2013-05-31 14:35:03

标签: c# unity3d

当我有多个敌人时,他们的健康值为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;
        }

    }


}

}

我试图找出是否有人遇到了一些问题,或者是否有解决方法,但我找不到解决方案或谷歌。所以有人可以告诉我,如果我做错了什么或是否有教程或其他什么。

1 个答案:

答案 0 :(得分:6)

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

staticcurrEnemyLifecurrEnemyBullets属性中删除currEnemyFuel修饰符。

然后,您需要更改Bullet中修改这些值的方式,因为您无法再通过EnemyAI直接访问这些值。我对Unity3D了解不多,但经过一些阅读后我认为你需要做这样的事情:

var enemy = otherObject.GetComponent<EnemyClass>();
enemy.currEnemyLife -= 50f;
// etc...