Search Unity

Third Party Photon Camera not fixing to instaniated player

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

  1. o0neza0o

    o0neza0o

    Joined:
    Sep 6, 2015
    Posts:
    165
    The player picks up the player but to me it seems it has something to do with the ID but not sure how to fix :/.

    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 = player;
    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.  

    CameraFollow

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraFollow : MonoBehaviour {
    6.  
    7.     public float interpVelocity;
    8.     public float minDistance;
    9.     public float followDistance;
    10.     public GameObject target;
    11.     public Vector3 offset;
    12.     public Vector3 targetPos;
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         targetPos = transform.position;
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void FixedUpdate () {
    21.         if (target)
    22.         {
    23.             Vector3 posNoZ = transform.position;
    24.             posNoZ.z = target.transform.position.z;
    25.  
    26.             Vector3 targetDirection = (target.transform.position - posNoZ);
    27.  
    28.             interpVelocity = targetDirection.magnitude * 5f;
    29.  
    30.             targetPos = transform.position + (targetDirection.normalized * interpVelocity * Time.deltaTime);
    31.  
    32.             transform.position = Vector3.Lerp( transform.position, targetPos + offset, 0.25f);
    33.  
    34.         }
    35.     }
    36. }
    37.  
    38.  
    39.