Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party [Solved] Sync RPC's for joining players using Photon

Discussion in 'Multiplayer' started by Leiszner, Jan 4, 2016.

  1. Leiszner

    Leiszner

    Joined:
    Aug 31, 2013
    Posts:
    3
    Hi

    First of all sorry for my english, and also im not that good of a programmer.

    So im working on a multiplayer game using Photon. The idea is you can choose weapon in a looby. I switch scene for the players as they join a room through Photon. It works fine for the players, that are connected at the time you choose the weapon. But for the players who join later, or after another player have choosen weapon, can't see the change/current weapon. My script looks like this, and is placed on every player that joins.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerBehavior : Photon.MonoBehaviour
    5. {
    6.     #region Variables
    7.  
    8.     //Private
    9.     private float playerSpeed = 5;
    10.     private float lastSynchronizationTime = 0f;
    11.     private float syncDelay = 0f;
    12.     private float syncTime = 0f;
    13.  
    14.     private Transform playerLastStep;
    15.  
    16.     private Vector3 syncStartPosition = Vector3.zero;
    17.     private Vector3 syncEndPosition = Vector3.zero;
    18.     private Quaternion syncStartRotation = Quaternion.identity;
    19.     private Quaternion syncEndRotation = Quaternion.identity;
    20.  
    21.     private GameObject othersWeapon;
    22.  
    23.     //Public
    24.     public GameObject[] WeaponArray;
    25.  
    26.     //Static
    27.  
    28.     #endregion
    29.  
    30.     #region Methods
    31.  
    32.     void Start()
    33.     {
    34.         //Does so you start with no weapon
    35.         if (photonView.isMine)
    36.         {
    37.             for (int i = 0; i < WeaponArray.Length; i++)
    38.             {
    39.                 WeaponArray.SetActive(false);
    40.             }
    41.         }
    42.  
    43.         playerLastStep = gameObject.transform;
    44.     }
    45.  
    46.     void Update()
    47.     {
    48.         if (photonView.isMine)
    49.         {
    50.             Movement();
    51.         }
    52.         else
    53.         {
    54.             SyncedMovement();
    55.         }
    56.     }
    57.  
    58.     void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    59.     {
    60.         if (stream.isWriting)
    61.         {
    62.             stream.SendNext(transform.position);
    63.             stream.SendNext(transform.rotation);
    64.         }
    65.         else
    66.         {
    67.             syncEndPosition = (Vector3)stream.ReceiveNext();
    68.             syncEndRotation = (Quaternion)stream.ReceiveNext();
    69.  
    70.             syncStartRotation = transform.rotation;
    71.             syncStartPosition = transform.position;
    72.  
    73.             syncTime = 0f;
    74.             syncDelay = Time.time - lastSynchronizationTime;
    75.             lastSynchronizationTime = Time.time;
    76.         }
    77.     }
    78.  
    79.     private void SyncedMovement()
    80.     {
    81.         syncTime += Time.deltaTime;
    82.         transform.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
    83.         transform.rotation = Quaternion.Slerp(syncStartRotation, syncEndRotation, syncTime / syncDelay);
    84.     }
    85.  
    86.     void Movement()
    87.     {
    88.         //Player Movement
    89.         if (gameObject.transform.position.x >= -34 && gameObject.transform.position.x <= 34 && gameObject.transform.position.y <= 18
    90.             && gameObject.transform.position.y >= -18)
    91.         {
    92.             GetComponent<Rigidbody2D>().velocity = new Vector3(Input.GetAxis("L_Joy_X") * playerSpeed, -Input.GetAxis("L_Joy_Y") * playerSpeed, 0);
    93.         }
    94.  
    95.         //Player Rotation
    96.         if (Input.GetAxis("R_Joy_X") > 0.5f || Input.GetAxis("R_Joy_Y") > 0.5f ||
    97.             Input.GetAxis("R_Joy_X") < -0.5f || Input.GetAxis("R_Joy_Y") < -0.5f)
    98.         {
    99.             float angle = Mathf.Atan2(-Input.GetAxis("R_Joy_X"), -Input.GetAxis("R_Joy_Y")) * Mathf.Rad2Deg;
    100.             Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
    101.             transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * 10);
    102.  
    103.             playerLastStep = gameObject.transform;
    104.         }
    105.  
    106.         if (Input.GetAxis("R_Joy_X") == 0 && Input.GetAxis("R_Joy_Y") == 0)
    107.         {
    108.             gameObject.transform.rotation = playerLastStep.transform.rotation;
    109.         }
    110.     }
    111.  
    112.     void OnCollisionEnter2D(Collision2D other)
    113.     {
    114.         //Detects Which Weapon You've choosen
    115.         if (other.gameObject.tag == "Spearrack")
    116.         {
    117.             CurrentWeapon("Spear");
    118.         }
    119.  
    120.         if (other.gameObject.tag == "Swordrack")
    121.         {
    122.             CurrentWeapon("Sword");
    123.         }
    124.  
    125.         if (other.gameObject.tag == "Swallowrack")
    126.         {
    127.             CurrentWeapon("Swallow");
    128.         }
    129.     }
    130.  
    131.     void CurrentWeapon(string weaponName)
    132.     {
    133.         //Sets the currentweapon
    134.         if (weaponName == "Spear")
    135.         {
    136.             GetComponent<PhotonView>().RPC("SpearActive", PhotonTargets.All, null);
    137.         }
    138.         else
    139.         {
    140.             GetComponent<PhotonView>().RPC("SpearDeactive", PhotonTargets.All, null);
    141.         }
    142.  
    143.         if (weaponName == "Sword")
    144.         {
    145.             GetComponent<PhotonView>().RPC("SwordActive", PhotonTargets.All, null);
    146.         }
    147.         else
    148.         {
    149.             GetComponent<PhotonView>().RPC("SwordDeactive", PhotonTargets.All, null);
    150.         }
    151.  
    152.         if (weaponName == "Swallow")
    153.         {
    154.             GetComponent<PhotonView>().RPC("SwallowActive", PhotonTargets.All, null);
    155.         }
    156.         else
    157.         {
    158.             GetComponent<PhotonView>().RPC("SwallowDeactive", PhotonTargets.All, null);
    159.         }
    160.     }
    161.  
    162.     [PunRPC]
    163.     void SpearActive()
    164.     {
    165.         WeaponArray[0].SetActive(true);
    166.     }
    167.  
    168.     [PunRPC]
    169.     void SpearDeactive()
    170.     {
    171.         WeaponArray[0].SetActive(false);
    172.     }
    173.  
    174.     [PunRPC]
    175.     void SwordActive()
    176.     {
    177.         WeaponArray[1].SetActive(true);
    178.     }
    179.  
    180.     [PunRPC]
    181.     void SwordDeactive()
    182.     {
    183.         WeaponArray[1].SetActive(false);
    184.     }
    185.  
    186.     [PunRPC]
    187.     void SwallowActive()
    188.     {
    189.         WeaponArray[2].SetActive(true);
    190.     }
    191.  
    192.     [PunRPC]
    193.     void SwallowDeactive()
    194.     {
    195.         WeaponArray[2].SetActive(false);
    196.     }
    197.  
    198.     #endregion
    199. }
    Thank you for your help :)
     
    Last edited: Jan 6, 2016
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    Leiszner likes this.
  3. Leiszner

    Leiszner

    Joined:
    Aug 31, 2013
    Posts:
    3
  4. Leiszner

    Leiszner

    Joined:
    Aug 31, 2013
    Posts:
    3
    So i found the problem. Simply by replace ' PhotonTargets.All ' with ' PhotonTargets.AllBuffered ' it all works perfectly fine. Also for players joining later on.
     
    AcerRacer, BdarVirtu and tobiass like this.