Search Unity

Third Party Photon Camera won't pick up the instantiated player

Discussion in 'Multiplayer' started by o0neza0o, May 15, 2017.

  1. o0neza0o

    o0neza0o

    Joined:
    Sep 6, 2015
    Posts:
    165
    The network script that i am using is this -

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine.UI;
    6.  
    7. /// <summary>
    8. /// This script automatically connects to Photon (using the settings file),
    9. /// tries to join a random room and creates one if none was found (which is ok).
    10. /// </summary>
    11. public class ConnectAndJoinRandom : Photon.MonoBehaviour
    12. {
    13.     /// <summary>Connect automatically? If false you can set this to true later on or call ConnectUsingSettings in your own scripts.</summary>
    14.     public bool AutoConnect = true;
    15.     public GameObject player;
    16.     public Cell cell;
    17.     public Camera ourCamera;
    18.     [SerializeField]
    19.     private GameObject[] spawnPoints;
    20.     public byte Version = 1;
    21.  
    22.     /// <summary>if we don't want to connect in Start(), we have to "remember" if we called ConnectUsingSettings()</summary>
    23.     private bool ConnectInUpdate = true;
    24.  
    25.  
    26.     public virtual void Start()
    27.     {
    28.         PhotonNetwork.autoJoinLobby = true;    // we join randomly. always. no need to join a lobby to get the list of rooms.
    29.     }
    30.  
    31.     public virtual void Update()
    32.     {
    33.         if (ConnectInUpdate && AutoConnect && !PhotonNetwork.connected)
    34.         {
    35.             Debug.Log("Update() was called by Unity. Scene is loaded. Let's connect to the Photon Master Server. Calling: PhotonNetwork.ConnectUsingSettings();");
    36.  
    37.             ConnectInUpdate = true;
    38.             PhotonNetwork.ConnectUsingSettings(Version + "0.1" + SceneManagerHelper.ActiveSceneBuildIndex);
    39.         }
    40.     }
    41.  
    42.  
    43.     // below, we implement some callbacks of PUN
    44.     // you can find PUN's callbacks in the class PunBehaviour or in enum PhotonNetworkingMessage
    45.  
    46.  
    47.     public virtual void OnConnectedToMaster()
    48.     {
    49.         Debug.Log("OnConnectedToMaster() was called by PUN. Now this client is connected and could join a room. Calling: PhotonNetwork.JoinRandomRoom();");
    50.         PhotonNetwork.JoinRandomRoom();
    51.     }
    52.  
    53.     public virtual void OnJoinedLobby()
    54.     {
    55.         Debug.Log("OnJoinedLobby(). This client is connected and does get a room-list, which gets stored as PhotonNetwork.GetRoomList(). This script now calls: PhotonNetwork.JoinRandomRoom();");
    56.         PhotonNetwork.JoinRandomRoom();
    57.     }
    58.  
    59.     public virtual void OnPhotonRandomJoinFailed()
    60.     {
    61.         Debug.Log("OnPhotonRandomJoinFailed() was called by PUN. No random room available, so we create one. Calling: PhotonNetwork.CreateRoom(null, new RoomOptions() {maxPlayers = 4}, null);");
    62.         PhotonNetwork.CreateRoom(null, new RoomOptions() { MaxPlayers = 4 }, null);
    63.     }
    64.  
    65.     // the following methods are implemented to give you some context. re-implement them as needed.
    66.  
    67.     public virtual void OnFailedToConnectToPhoton(DisconnectCause cause)
    68.     {
    69.         Debug.LogError("Cause: " + cause);
    70.     }
    71.  
    72.     public void OnJoinedRoom()
    73.     {
    74.         PhotonNetwork.Instantiate (player.transform.name, Vector3.zero, Quaternion.identity, 0);
    75.         player.GetComponent<Cell>().enabled = true;
    76.         GameObject ourCamera = GameObject.FindWithTag ("MainCamera");
    77.         if (ourCamera != null)
    78.         {
    79.             CameraFollow follow = ourCamera.GetComponent ("CameraFollow") as CameraFollow;
    80.             if (follow != null)
    81.             {
    82.                 follow.target = (GameObject)Network.Instantiate(player, Vector3.up * 5, Quaternion.identity, 0);
    83.             }
    84.         }
    85.  
    86.         player.GetComponent<Cell>().enabled = true;
    87.         Debug.Log("OnJoinedRoom() called by PUN. Now this client is in a room. From here on, your game would be running. For reference, all callbacks are listed in enum: PhotonNetworkingMessage");
    88.     }
    89.  
    90.  
    91.  
    92.  
    93. }
    94.  
    But for some reason it stil doesn't pick up the player/player ID

    this is the camera follow script

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class CameraFollow : Photon.MonoBehaviour {

    public float interpVelocity;
    public float minDistance;
    public float followDistance;
    public GameObject target;
    public Vector3 offset;
    public Vector3 targetPos;

    // Use this for initialization
    void Start () {

    targetPos = transform.position;
    }

    // Update is called once per frame
    void FixedUpdate () {

    if(photonView.isMine)
    {
    if (target)
    {
    Vector3 posNoZ = transform.position;
    posNoZ.z = target.transform.position.z;

    Vector3 targetDirection = (target.transform.position - posNoZ);

    interpVelocity = targetDirection.magnitude * 5f;

    targetPos = transform.position + (targetDirection.normalized * interpVelocity * Time.deltaTime);

    transform.position = Vector3.Lerp( transform.position, targetPos + offset, 0.25f);

    }
    }
    }

    }