Photon PUN Classic: How can I get a player to respawn when they die using RPC?
Options
So, i'm getting better at remembering some c# code, but I can't figure out how to add a respawn when they die.
I've tried to use PunRPC on 2 separate objects, the Player Prefab, and a Random Respawn Point, (because there's 8 on the map.) and i get errors. I have a Network Manager script on the Map Prefab, (Why? i honestly don't know...) and a Player Networking Script on the player.
Here's the code I have for the following scripts...
Network Manager Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NetworkManager : MonoBehaviour
{
[SerializeField] private Text connectText;
public GameObject spawnPoint;
public GameObject[] spawns;
[SerializeField] private GameObject player;
[SerializeField] private GameObject lobbyCamera;
// Start is called before the first frame update
void Start()
{
PhotonNetwork.ConnectUsingSettings("v0.1.1.3");
spawns = GameObject.FindGameObjectsWithTag("SpawnPoint");
}
private void Update()
{
connectText.text = PhotonNetwork.connectionStateDetailed.ToString();
spawnPoint = spawns[Random.Range(0, spawns.Length)];
}
public virtual void OnJoinedLobby()
{
Debug.Log("Connected To Lobby!");
RoomOptions roomOptions = new RoomOptions();
roomOptions.MaxPlayers = 4;
PhotonNetwork.JoinOrCreateRoom("NewRoom", null, null);
}
public virtual void OnJoinedRoom()
{
if (spawnPoint == null) {
Debug.LogError("Cannot Spawn Player!");
}
PhotonNetwork.Instantiate(player.name, spawnPoint.transform.position, spawnPoint.transform.rotation, 0);
lobbyCamera.SetActive(false);
}
}
Player Networking Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerNetworking : MonoBehaviour
{
[SerializeField] private GameObject playerCamera;
[SerializeField] private GameObject player;
[SerializeField] private MonoBehaviour[] playerControlScripts;
[SerializeField] private int playerHealth = 20;
[SerializeField] private GameObject spectateCamera;
private PhotonView photonView;
private void Start()
{
photonView = GetComponent();
Initialize();
spectateCamera = GameObject.FindGameObjectWithTag("SpecCam");
}
private void Initialize()
{
if(photonView.isMine)
{
spectateCamera.SetActive(false);
}
else
{
playerCamera.SetActive(false);
foreach (MonoBehaviour m in playerControlScripts)
{
m.enabled = false;
}
}
}
// private void Update()
//{
//if (!photonView.isMine)
//{
// return;
//}
//}
private void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(playerHealth);
}
else if (stream.isReading)
{
playerHealth = (int)stream.ReceiveNext();
}
}
[PunRPC]
public void ApplyDamage(int damage)
{
playerHealth -= damage;
if (playerHealth <= 0)
{
Die();
}
}
public void Die()
{
}
}
Help would be greatly appreciated. Thank you!
-Delta (CodeRedGames)
I've tried to use PunRPC on 2 separate objects, the Player Prefab, and a Random Respawn Point, (because there's 8 on the map.) and i get errors. I have a Network Manager script on the Map Prefab, (Why? i honestly don't know...) and a Player Networking Script on the player.
Here's the code I have for the following scripts...
Network Manager Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NetworkManager : MonoBehaviour
{
[SerializeField] private Text connectText;
public GameObject spawnPoint;
public GameObject[] spawns;
[SerializeField] private GameObject player;
[SerializeField] private GameObject lobbyCamera;
// Start is called before the first frame update
void Start()
{
PhotonNetwork.ConnectUsingSettings("v0.1.1.3");
spawns = GameObject.FindGameObjectsWithTag("SpawnPoint");
}
private void Update()
{
connectText.text = PhotonNetwork.connectionStateDetailed.ToString();
spawnPoint = spawns[Random.Range(0, spawns.Length)];
}
public virtual void OnJoinedLobby()
{
Debug.Log("Connected To Lobby!");
RoomOptions roomOptions = new RoomOptions();
roomOptions.MaxPlayers = 4;
PhotonNetwork.JoinOrCreateRoom("NewRoom", null, null);
}
public virtual void OnJoinedRoom()
{
if (spawnPoint == null) {
Debug.LogError("Cannot Spawn Player!");
}
PhotonNetwork.Instantiate(player.name, spawnPoint.transform.position, spawnPoint.transform.rotation, 0);
lobbyCamera.SetActive(false);
}
}
Player Networking Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerNetworking : MonoBehaviour
{
[SerializeField] private GameObject playerCamera;
[SerializeField] private GameObject player;
[SerializeField] private MonoBehaviour[] playerControlScripts;
[SerializeField] private int playerHealth = 20;
[SerializeField] private GameObject spectateCamera;
private PhotonView photonView;
private void Start()
{
photonView = GetComponent();
Initialize();
spectateCamera = GameObject.FindGameObjectWithTag("SpecCam");
}
private void Initialize()
{
if(photonView.isMine)
{
spectateCamera.SetActive(false);
}
else
{
playerCamera.SetActive(false);
foreach (MonoBehaviour m in playerControlScripts)
{
m.enabled = false;
}
}
}
// private void Update()
//{
//if (!photonView.isMine)
//{
// return;
//}
//}
private void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(playerHealth);
}
else if (stream.isReading)
{
playerHealth = (int)stream.ReceiveNext();
}
}
[PunRPC]
public void ApplyDamage(int damage)
{
playerHealth -= damage;
if (playerHealth <= 0)
{
Die();
}
}
public void Die()
{
}
}
Help would be greatly appreciated. Thank you!
-Delta (CodeRedGames)
0
Comments
-
Update: I figured out how to get respawning to work. Thank you for reading this! ; )0
-
Nice!
I made a Example Project with Photon Classic, if you need some ideas https://github.com/SradnickDev/Photon-Example just take a look.0