Search Unity

Third Party Photon Pun 2 Player View

Discussion in 'Multiplayer' started by cirazgames, Jul 24, 2021.

  1. cirazgames

    cirazgames

    Joined:
    Apr 13, 2021
    Posts:
    11
    I have an issue that occurs after spawning 2 Players.
    I connect in the Editor first, and then spawn the first Player in a Map, which works fine until here.
    But then, when I spawn in a second Player, from my mobile device, I can see the two Players in the Editor, but on my mobile I see just 1 Player.

    The Player Character has these components:
    Rigidbody, Capsule Collider, SingleAttack (Script), AnimalTigerRoar (Script),
    PhotonView, PhotonTransformView, PhotonRigidbodyView, PhotonAnimatorView,
    and a custom Script I created for synchronizing the Player´s position/rotation/animations, named "NetworkPlayerPhoton".

    This is the Script:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using Photon.Pun;
    4. using Photon.Realtime;
    5. using System.Collections;
    6. using System.Collections.Generic;
    7.  
    8. public class NetworkPlayerPhoton : MonoBehaviourPun, IPunObservable
    9. {
    10.     private Transform character;
    11.     private Transform personalCamera;
    12.     private JoystickController joystickController;
    13.     private SingleAttack singleAttack;
    14.     private AnimalTigerRoar animalRoar;
    15.  
    16.     //Fix lag
    17.     private Rigidbody r;
    18.  
    19.     //For smoothing the Movement of other Characters
    20.     Vector3 latestPos;
    21.     Quaternion latestRot;
    22.     Vector3 velocity;
    23.     //Vector3 angularVelocity;
    24.  
    25.     bool valuesReceived = false;
    26.  
    27.     //Link the Animator
    28.     private Animator animator;
    29.  
    30.  
    31.     private void Start()
    32.     {
    33.         if (photonView.IsMine)
    34.         {
    35.             personalCamera.gameObject.SetActive(true);
    36.             joystickController.GetComponent<JoystickController>().enabled = true;
    37.             GetComponent<SingleAttack>().enabled = true;
    38.             GetComponent<AnimalTigerRoar>().enabled = true;
    39.         }
    40.         else
    41.         {
    42.             Destroy(character);
    43.             Destroy(this);
    44.         }
    45.     }
    46.  
    47.     private void FixedUpdate()
    48.     {
    49.         GetNeededComponents();
    50.         Start();
    51.     }
    52.  
    53.     private void GetNeededComponents()
    54.     {
    55.         GameObject temporaryPlayer = GameObject.FindGameObjectWithTag("Player");
    56.         character = temporaryPlayer.transform.GetChild(0);
    57.  
    58.         //GameObject temporaryCamera = GameObject.FindGameObjectWithTag("Camera");
    59.         //personalCamera = temporaryCamera.transform.GetChild(0);
    60.         personalCamera = GameObject.Find("-----ThirdPersonCamera-----").transform.Find("FreeLookCameraRig");
    61.  
    62.         GameObject temporaryJoystick = GameObject.FindGameObjectWithTag("Joystick");
    63.         joystickController = temporaryJoystick.GetComponentInChildren<JoystickController>();
    64.  
    65.         singleAttack = GetComponent<SingleAttack>();
    66.  
    67.         animalRoar = GetComponent<AnimalTigerRoar>();
    68.  
    69.         animator = GetComponent<Animator>();
    70.  
    71.         r = GetComponent<Rigidbody>();
    72.     }
    73.  
    74.  
    75.     //Send the Character Position and Rotation through the Server
    76.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    77.     {
    78.         if (stream.IsWriting)
    79.         {
    80.             // We own this player: send the others our data
    81.             stream.SendNext(transform.position);
    82.             stream.SendNext(transform.rotation);
    83.             stream.SendNext(r.position);
    84.             stream.SendNext(r.rotation);
    85.             stream.SendNext(r.velocity);
    86.             //stream.SendNext(r.angularVelocity);
    87.             stream.SendNext(animator.GetBool("Walk"));
    88.             stream.SendNext(animator.GetBool("Run"));
    89.             stream.SendNext(animator.GetBool("Sound"));
    90.             stream.SendNext(animator.GetBool("Attack"));
    91.         }
    92.         else
    93.         {
    94.             // Network player, receive data
    95.             latestPos = (Vector3)stream.ReceiveNext();
    96.             latestRot = (Quaternion)stream.ReceiveNext();
    97.             r.position = (Vector3)stream.ReceiveNext();
    98.             r.rotation = (Quaternion)stream.ReceiveNext();
    99.             r.velocity = (Vector3)stream.ReceiveNext();
    100.             //r.angularVelocity = (Vector3)stream.ReceiveNext();
    101.             animator.SetBool("Walk", (bool)stream.ReceiveNext());
    102.             animator.SetBool("Run", (bool)stream.ReceiveNext());
    103.             animator.SetBool("Sound", (bool)stream.ReceiveNext());
    104.             animator.SetBool("Attack", (bool)stream.ReceiveNext());
    105.  
    106.             float lag = Mathf.Abs((float)(PhotonNetwork.Time - info.SentServerTime));
    107.             r.position += r.velocity * lag;
    108.  
    109.             valuesReceived = true;
    110.         }
    111.     }
    112.  
    113.     private void Update()
    114.     {
    115.         if (!photonView.IsMine && valuesReceived)
    116.         {
    117.             //Update Object position and Rigidbody parameters
    118.             transform.position = Vector3.Lerp(transform.position, latestPos, Time.deltaTime * 5);
    119.             transform.rotation = Quaternion.Lerp(transform.rotation, latestRot, Time.deltaTime * 5);
    120.             r.velocity = velocity;
    121.             //r.angularVelocity = angularVelocity;
    122.         }
    123.     }
    124.  
    125.     void OnCollisionEnter(Collision contact)
    126.     {
    127.         if (!photonView.IsMine)
    128.         {
    129.             Transform collisionObjectRoot = contact.transform.root;
    130.             if (collisionObjectRoot.CompareTag("MeshPlayer"))
    131.             {
    132.                 //Transfer PhotonView of Rigidbody to our local player
    133.                 photonView.TransferOwnership(PhotonNetwork.LocalPlayer);
    134.             }
    135.         }
    136.     }
    137. }
    138.