所以我有一个玩家健康脚本,它有一个函数被调用来收回脚本所在玩家的健康点。
因此 PlayerOnes 健康脚本将跟踪他的健康 PlayerTwo 将跟踪他的健康。
当检测到子弹碰撞时,它会从碰撞对象的健康脚本中调用 takedamage。因此它会受到伤害,它适用于 NPC,但不适用于联网玩家。
如果 P1 射击 ---> P2 两次,它会受到 50 的伤害,然后再次受到 50 的伤害,但它不会输出你的死亡,也不会记录玩家的命中,这意味着它不会受到伤害,因为玩家生命值是 0,因此它只是检测到碰撞而不做任何事情。但它不会执行其余代码并转换玩家位置等。问题是脚本在两台计算机上运行并且在两台计算机上都检测到碰撞,因此无论它是否联网,它都应该检测到它并执行更新并注册它。
有任何想法吗?
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour {
public float hitPoints = 100;
public Transform explosionPrefab;
private Transform explosion;
public float currentHitPoints;
SpawnSpot[] spawnSpots;
private bool dead = false;
GameObject deadZone;
// Use this for initialization
void Start () {
currentHitPoints = hitPoints;
spawnSpots = GameObject.FindObjectsOfType<SpawnSpot> ();
}
// Update is called once per frame
void Update () {
if (currentHitPoints <= 0) {
deadZone = GameObject.FindWithTag("DeadZone");
gameObject.transform.position = deadZone.transform.position;
dead = true;
Instantiate(explosionPrefab, gameObject.transform.position, gameObject.transform.rotation);
}
if (dead) {
if (currentHitPoints <= 0){
//if (gameObject.transform.name == "Player"{
SpawnSpot mySpawnSpot = spawnSpots [Random.Range (0, spawnSpots.Length)];
Debug.Log (gameObject.transform.name + "Your DEAD");
if (Input.GetButtonDown ("Fire1")) {
gameObject.transform.position = mySpawnSpot.transform.position;
currentHitPoints = 100;
}
}
}
}
[RPC]
public void TakeDamage(float amt){
currentHitPoints -= amt;
Debug.Log ("taking damage");
if (currentHitPoints <= 0) {
Die();
}
}