统一;变量影响一般 Class 而不是附加了 Class 的特定游戏对象,并且游戏对象变量永久存储值
Unity; Variable affects general Class rather then specific gameObject with Class attached, and gameObject variable stores value permanently
当 Player
用 Raycast
射击 Zombie
时,它应该会损坏 Zombie(clone)
直到它被摧毁。但是,Player
造成的伤害是对变量 currentHealth
的一般 Zombie
class 的伤害,而不是对每个 Zombie GameObject
的 currentHealth
的伤害。此外,即使游戏从头开始重新启动,currentHealth
值仍然存在。
public class Zombie : MonoBehaviour
{
public int currentHealth = 2;
public void Damage(int damageAmount)
{
//subtract damage amount when Damage function is called
currentHealth -= damageAmount;
//Check if health has fallen below zero
if (currentHealth <= 0)
{
//if health has fallen below zero, deactivate it
gameObject.SetActive (false);
//Destroy(gameObject);
}
Debug.Log(name + currentHealth);
}
}
--
public class Player : MonoBehaviour
{
public RaycastHit hit;
public int gunDamage = 1;
public Zombie zombie;
public Camera fpsCamera;
public int weaponRange = 50;
private int layerMask = 1 << 9;
void Start()
{
spawnPoints = playerSpawnPoint.GetComponentsInChildren<Transform>();
// bullet = GetComponent<GameObject>();
}
void LazerBeam()
{
Vector3 rayOrigin = fpsCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0f));
Debug.DrawRay(rayOrigin, Vector3.forward * weaponRange, Color.green);
if (Physics.Raycast(rayOrigin, fpsCamera.transform.forward, out hit, weaponRange, layerMask))
{
zombie.Damage(gunDamage); //kills zombie
Debug.Log(name + " " + zombie);
}
Debug.Log(hit.collider);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Z))
{
LazerBeam();
}
}
您目前正在对附加在玩家身上的 Zombie
施加伤害,而不是附加在被击中的物体上。您可以从 RaycastHit hit
获得对它的引用。
if (Physics.Raycast(rayOrigin, fpsCamera.transform.forward, out hit, weaponRange, layerMask))
{
Zombie zombieHit = hit.transform.gameObject.GetComponent<Zombie>();
zombieHit.Damage(gunDamage); //kills zombie
Debug.Log(name + " " + zombieHit);
}
当 Player
用 Raycast
射击 Zombie
时,它应该会损坏 Zombie(clone)
直到它被摧毁。但是,Player
造成的伤害是对变量 currentHealth
的一般 Zombie
class 的伤害,而不是对每个 Zombie GameObject
的 currentHealth
的伤害。此外,即使游戏从头开始重新启动,currentHealth
值仍然存在。
public class Zombie : MonoBehaviour
{
public int currentHealth = 2;
public void Damage(int damageAmount)
{
//subtract damage amount when Damage function is called
currentHealth -= damageAmount;
//Check if health has fallen below zero
if (currentHealth <= 0)
{
//if health has fallen below zero, deactivate it
gameObject.SetActive (false);
//Destroy(gameObject);
}
Debug.Log(name + currentHealth);
}
}
--
public class Player : MonoBehaviour
{
public RaycastHit hit;
public int gunDamage = 1;
public Zombie zombie;
public Camera fpsCamera;
public int weaponRange = 50;
private int layerMask = 1 << 9;
void Start()
{
spawnPoints = playerSpawnPoint.GetComponentsInChildren<Transform>();
// bullet = GetComponent<GameObject>();
}
void LazerBeam()
{
Vector3 rayOrigin = fpsCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0f));
Debug.DrawRay(rayOrigin, Vector3.forward * weaponRange, Color.green);
if (Physics.Raycast(rayOrigin, fpsCamera.transform.forward, out hit, weaponRange, layerMask))
{
zombie.Damage(gunDamage); //kills zombie
Debug.Log(name + " " + zombie);
}
Debug.Log(hit.collider);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Z))
{
LazerBeam();
}
}
您目前正在对附加在玩家身上的 Zombie
施加伤害,而不是附加在被击中的物体上。您可以从 RaycastHit hit
获得对它的引用。
if (Physics.Raycast(rayOrigin, fpsCamera.transform.forward, out hit, weaponRange, layerMask))
{
Zombie zombieHit = hit.transform.gameObject.GetComponent<Zombie>();
zombieHit.Damage(gunDamage); //kills zombie
Debug.Log(name + " " + zombieHit);
}