- Home /
Multiplayer Respawning Problems
So I've made a script that says when the player dies (whether it be me (the host) or the other player (the client) it's going to be teleported up to a place called the "prison" and after a set number of seconds he's going to respawn at the original map in a random spawn point. In theory, this works, and I've seen it work for the host. Now, the problem is, the host now respawns instantly whilst the client player gets stuck up at the prison and doesn't come down. I've been pondering about this for ages and it's getting on my nerves, here's the code, hope someone might be able to give me some advice:
using UnityEngine; using System.Collections; using UnityEngine.Networking; using UnityEngine.UI;
public class Target : NetworkBehaviour { [SerializeField] Slider healthbar; public float maxhealth; public float currenthealth;
public float damage = 10f;
private void Awake()
{
healthbar = gameObject.GetComponentInChildren<Slider>();
}
public void Start()
{
maxhealth = 50f;
healthbar.maxValue = maxhealth;
healthbar.value = healthbar.maxValue;
currenthealth = healthbar.value;
}
[Command]
public void CmdDamageOther(NetworkInstanceId t)
{
NetworkServer.FindLocalObject(t).GetComponent<Target>().RpcTakeDamage();
}
[ClientRpc]
public void RpcTakeDamage()
{
currenthealth -= damage;
if (currenthealth <= 0f)
{
RpcDie();
}
}
[Command]
public void CmdTakeDamage()
{
Debug.Log("Client is buggin meee");
currenthealth -= damage;
if (currenthealth <= 0f)
{
RpcDie();
}
}
void Update()
{
healthbar.value = currenthealth;
if (currenthealth <= 0f)
{
RpcDie();
}
}
[ClientRpc]
void RpcDie()
{
if (isLocalPlayer)
{
gameObject.GetComponentInChildren<Shooting>().istrue = false;
transform.position = GameObject.Find("Prison").transform.position;
Debug.Log("you been up");
StartCoroutine(Call());
}
}
IEnumerator Call()
{
CmdRespawn();
yield return new WaitForSeconds(3.0f);
gameObject.GetComponentInChildren<Shooting>().istrue = true;
}
[Command]
void CmdRespawn()
{
healthbar.value = healthbar.maxValue;
currenthealth = healthbar.maxValue;
transform.position = new Vector3(0,0,0) + new Vector3(Random.Range(-300,300),10,Random.Range(-300,300));
StopAllCoroutines();
}
}
Your answer
Follow this Question
Related Questions
Multiplayer Respawning Problems 0 Answers
UNET player respawn 1 Answer
Re-hosting a Match in unity creates a bunch of random errors 1 Answer