- Home /
Question by
michaelnorth77 · Feb 27, 2018 at 01:58 AM ·
multiplayerspawningdeathrespawning
In My Multiplayer Game only he host Respawns when he dies
I am making a simple multiplayer game, but when the client player dies, he does not respawn, he just sits there with 0 health. What should I do to make it work?
Here's my code:
public GameObject tank; public Canvas deathCanvas;
[SyncVar (hook = "OnHealthChanged")]private int health = 100;
private Text healthText;
public RectTransform healthBar;
private NetworkStartPosition[] spawnPoints;
// Use this for initialization
void Start ()
{
if (isLocalPlayer) {
spawnPoints = FindObjectsOfType<NetworkStartPosition> ();
}
healthText = GameObject.Find ("HealthText").GetComponent<Text> ();
SetHealthText ();
}
void Update ()
{
if (health <= 0 && isLocalPlayer)
{
tank.SetActive (false);
tank.GetComponent<MeshRenderer> ().enabled = false;
RpcRespawn ();
tank.SetActive (true);
tank.GetComponent<MeshRenderer> ().enabled = true;
health = 100;
}
}
void SetHealthText()
{
if(isLocalPlayer)
{
healthText.text = health.ToString ();
}
}
public void DeductHealth (int dmg)
{
health -= dmg;
}
void OnHealthChanged(int hp)
{
healthBar.sizeDelta = new Vector2 (hp * 2, healthBar.sizeDelta.y);
health = hp;
SetHealthText ();
}
[ClientRpc]
void RpcRespawn ()
{
if (isLocalPlayer) {
Vector3 spawnPoint = Vector3.zero;
if (spawnPoints != null && spawnPoints.Length > 0)
{
spawnPoint = spawnPoints [Random.Range (0, spawnPoints.Length)].transform.position;
}
transform.position = spawnPoint;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
UNET - Players Spawn With Misplaced Authority 1 Answer
Multiplayer Spawning the Client on the fixed locations, i.e. override Round robin as well as Random 0 Answers
Networking Question: Spawning selectively / on only one client. 1 Answer
UNET Clients spawn Childs on Player & Client Sync problems. 0 Answers
error CS1502 and CS1503 1 Answer