Troubles with player skin when respawning

Options
Hello,
First I want to say that I apologize if this question has already been answered and I just hadn't seen it. I did try my best to look at anything appearing to be similar before posting.

So I may have a bit of an odd setup going on.. Instead of just spawning the players alone, I have it so that after the initial players are spawned, the player you spawn as will be the player that you will play as for the entirety of time you are playing the round and instead each player spawns a skin. The idea is that first the player spawns, then that player spawns a skin to serve as the avatar which takes the damage and does the dying etc..
If your skin dies, your player's position is reset to a spawnPoint and a new skin is spawned.
I have all of this working fine locally, but the issue I'm having happens after all players are spawned. If one of the player's skins dies, instead of spawning a new skin it seems as if it just steals the skin from another player. :sweat_smile:
I'm guessing it has something to do with photonView ownership, or having a good way to tell it which skin goes where, but honestly I'm still very new to coding and I'm at a bit of a loss for how I should best proceed. If anyone has a suggestion I would very much appreciate the advice! :smile:

Here is the code that I'm currently using for my PlayerManager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRTK;
using RootMotion.FinalIK;
using NobleMuffins.LimbHacker;
​
​
public class PlayerManager : Photon.PunBehaviour
{
​
    public delegate void OnCharacterInstantiated(GameObject character);
    public static event OnCharacterInstantiated CharacterInstantiated;
​
​
    public NameControl NameControlerReference;
​
    //[Space(25)]
    //public GameObject childToThisObject;
​
    [Space(25)]
   // private GameObject followThisObject;
​
    [Space(25)]
    public GameObject playerSkin;
​
    [Space(25)]
    public GameObject playerSkin_Clone;
​
    [Space(50)]
    private GameObject[] spawns;
​
    [Space(50)] // VRiK Targets
    public GameObject Alias_Head;
    public GameObject Alias_Controller_Target_Object_Left;
    public GameObject Alias_Controller_Target_Object_Right;
​
    [Space(90)]
    private Transform trans;
​
    [Space(10)]
    public GameObject Camerarig_Reference;
​
    [Space(10)]
    public float respawnTimer = 0f;
​
    [Space(10)]
    public GameObject mainPlayer;
​
    [Space(10)]
    public GameObject mainPlayer_Clone;
​
​
    public void InstantiatePlayerSkin(int idx, int teamID)
    {
        if (playerSkin_Clone == null)
        {
            GameObject followThisObject = GameObject.FindGameObjectWithTag("BodyFollow_Tag").transform.gameObject;
​
            GameObject playerSkin_Clone = (GameObject) PhotonNetwork.Instantiate(playerSkin.name, followThisObject.transform.position, followThisObject.transform.rotation, 0);
​
            playerSkin_Clone.tag = "SkinClone_Tag";
​
            playerSkin_Clone.AddComponent<VRTK_TransformFollow>();
​
​
            Component[] listOfComponents_TransformFollow = playerSkin_Clone.GetComponentsInChildren<VRTK_TransformFollow>(true);
            foreach (VRTK_TransformFollow TransFlw_Scrpt in listOfComponents_TransformFollow)
            {
                TransFlw_Scrpt.enabled = true;
                TransFlw_Scrpt.gameObjectToFollow = followThisObject.gameObject;
                TransFlw_Scrpt.followsPosition = true;
                TransFlw_Scrpt.followsRotation = true;
                TransFlw_Scrpt.followsScale = false;
                TransFlw_Scrpt.moment = VRTK_TransformFollow.FollowMoment.OnPreCull;
            }
​
            Alias_Head = GameObject.FindGameObjectWithTag("Head_Target_Tag").transform.gameObject;
            Alias_Controller_Target_Object_Left = GameObject.FindGameObjectWithTag("Lhand_Target_Tag").transform.gameObject;
            Alias_Controller_Target_Object_Right = GameObject.FindGameObjectWithTag("Rhand_Target_Tag").transform.gameObject;
​
​
            Component[] listOfComponents_VRIK = playerSkin_Clone.GetComponentsInChildren<VRIK>();
            foreach (VRIK TVRiK_Scrpt in listOfComponents_VRIK)
            {
                TVRiK_Scrpt.enabled = true;
                TVRiK_Scrpt.solver.rightArm.target = Alias_Controller_Target_Object_Right.transform;
                TVRiK_Scrpt.solver.leftArm.target = Alias_Controller_Target_Object_Left.transform;
                TVRiK_Scrpt.solver.spine.headTarget = Alias_Head.transform;
            }
        }
    }
​
​
    void Awake()
    {
        if (mainPlayer == null)
        {
            Debug.LogError("MyNetworkManager is missing a reference to the player avatar prefab!");
        }
        
        spawns = GameObject.FindGameObjectsWithTag("Respawn");
    }
​
    public override void OnJoinedRoom()
    {
        NewPlayer(0);
        InstantiatePlayerSkin(0, 0);
    }
​
    public override void OnPhotonPlayerConnected(PhotonPlayer newPlayer)
    {
        if (PhotonNetwork.isMasterClient)
        {
            var idx = PhotonNetwork.otherPlayers.Length;
​
            photonView.RPC("NewPlayer", newPlayer, idx);
        }
    }

// Respawn timer
    public void Update()
    {
        if (respawnTimer > 0f)
        {
            respawnTimer -= Time.deltaTime;
            if (respawnTimer <= 0f)
            {
                InstantiatePlayerSkin(0, 0);
​
         playerSkin_Clone.transform.SetParent(mainPlayer_Clone.transform);
            }
        }
    }
​
[PunRPC]
    void NewPlayer(int idx)
    {
        trans = spawns[idx].transform;
​
        mainPlayer_Clone = PhotonNetwork.Instantiate(mainPlayer.name, trans.position, trans.rotation, 0);
​
        mainPlayer_Clone.name = "Player" + (idx + 1);
​
        mainPlayer_Clone.tag = "MainPlayer_Tag";
​
Component[] listOfComponents_CamRigSync = mainPlayer_Clone.GetComponentsInChildren<AvatarCameraRigSync>(true);
        foreach (AvatarCameraRigSync CamRigSync_Scrpt in listOfComponents_CamRigSync)
        {
            CamRigSync_Scrpt.enabled = true;
        }
    }
​}


Hopefully this is enough to make sense. Basically what it does is spawns the initial player and player skin, then if the player dies it triggers the respawn timer which in turn spawns a new player skin.

Thanks so much in advance to anyone who may be able to help! :smile:
(Bonus points if you can help me figure out a way to parent objects over the network that doesn't totally suck...lol)

-Jim

Comments

  • Jimbones
    Options
    Oh gosh...this is really embarrassing but I discovered that this wasn't the issue at all. As it turns out what actually was happening was all players were respawning a new skin on Die(). I had just neglected to add an if(photonView.isMine){} to my player's death script. Now all is well. :smile:
  • Jimbones
    Options
    Oh gosh...this is really embarrassing but I discovered that this wasn't the issue at all. As it turns out what actually was happening was all players were respawning a new skin on Die() and I had neglected to add an if(photonView.isMine){} to my player's death script.
This discussion has been closed.