Luc*_*cia 3 c# scripting unity-game-engine unity5
我在Unity中创建的游戏存在问题.玩家控制一个被一大群僵尸攻击的角色.我已经为所有的僵尸创造了一个产卵器并且效果很好,唯一的问题是,一旦玩家杀死一个僵尸,所有的僵尸都会从游戏世界中消失.我发布了附在下面每个僵尸上的敌人脚本.我无法弄清楚为什么每个僵尸都被摧毁而不仅仅是被攻击的僵尸.任何帮助都会很棒!
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public static float Damage = 10.0f;
public static float Health = 10.0f;
public Transform target;
public float Speed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Destroy the enemy if it's health reaches 0
if(Health <= 0){
Destroy(this.gameObject);
Debug.Log ("Enemy Destroyed!");
}
//Constantly move the enemy towards the centre of the gamespace (where the base is)
float step = Speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
Run Code Online (Sandbox Code Playgroud)
设置场景的方式是我有一个空的游戏对象,其中包含一系列位置对象和一个将敌人精灵放入位置对象的生成器脚本.这一切似乎都很好,但我找不到什么导致它们全部消失.
问题是您已声明Health为静态变量.这意味着Health将在所有敌人实例中具有相同的值.相反,声明健康状况:
public float Health = 10.0f;
Run Code Online (Sandbox Code Playgroud)
这样,每个实例化的敌人将能够拥有自己的唯一Health值.
| 归档时间: |
|
| 查看次数: |
875 次 |
| 最近记录: |