- Home /
Question by
kariemhassan2009 · Feb 11 at 03:58 AM ·
photonmultiplayer-networkingupdate functionfallinghealth
cant make player die when he falls
so here is my code my problem is that the in the update method some the code that's written isn't working and also i checked if the player is below -16 when he falls and he is pls help
`/okay here it is` / / /it starts here
using UnityEngine;
using System.Collections;
using Photon.Pun;
using Photon.Realtime;
public class Health : MonoBehaviourPunCallbacks, IPunObservable
{
public bool canDie = true;
public int maxHealth = 100;
public int currentHealth = 100;
public Transform transform;
public bool replaceWhenDead = false;
public GameObject deadReplacement;
public bool makeExplosion = false;
public GameObject explosion;
public bool isPlayer = false;
public GameObject deathCam;
private bool dead = false;
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(currentHealth);
}
else if (stream.IsReading)
{
currentHealth = (int)stream.ReceiveNext();
}
}
void Start()
{
// Initialize the currentHealth variable to the value specified by the user in startingHealth
currentHealth = maxHealth;
}
void Update()
{
if (transform.position.y <= -16)
{
Die();
}
}
public void ChangeHealth(int amount)
{
// Change the health by the amount specified in the amount variable
currentHealth += amount;
// If the health runs out, then Die.
if (currentHealth <= 0 && !dead && canDie)
Die();
// Make sure that the health never exceeds the maximum health
else if (currentHealth > maxHealth)
currentHealth = maxHealth;
}
IEnumerator Respawn()
{
transform.Find("Pm").GetComponent<Camera> ().enabled = false;
transform.position = new Vector3(-3, 1, 19);
yield return new WaitForSeconds(4);
transform.Find("Pm").GetComponent<Camera> ().enabled = true;
currentHealth = maxHealth;
dead = false;
}
public void Die()
{
// This GameObject is officially dead. This is used to make sure the Die() function isn't called again
dead = true;
// Make death effects
if (replaceWhenDead)
Instantiate(deadReplacement, transform.position, transform.rotation);
if (makeExplosion)
Instantiate(explosion, transform.position, transform.rotation);
if (isPlayer && deathCam != null)
deathCam.SetActive(true);
// Remove this GameObject from the scene
if(currentHealth <= 0)
{
StartCoroutine(Respawn());
}
}
}
Comment
Debug.Log transform.position.y, check if debug position equals to position. Maybe in Unity your player is child object of something and you are checking localposition instead.
Also if player's position is less than 16, then Die() method is going to be used each frame, since it's in Update, maybe im wrong since i didn't read all of your code carefully, but you probably should write !dead bool check in Update if statement.