【发布时间】:2017-07-16 21:21:20
【问题描述】:
当Player 用Raycast 射击Zombie 时,它应该会损坏Zombie(clone) 直到它被摧毁。但是,来自Player 的损坏会损坏变量currentHealth 上的一般Zombie 类,而不是每个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();
}
}
【问题讨论】:
标签: c# debugging inheritance unity3d gameobject