Search Unity

Third Party Photon Player problems

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

  1. o0neza0o

    o0neza0o

    Joined:
    Sep 6, 2015
    Posts:
    165
    I have it set up so players can auto connect to the room and tested it so far both players can join but cannot control their players :/.

    Code for camera control:

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

    public class Cell : Photon.MonoBehaviour {

    public Camera ourCamera ;

    public float Speed ;

    private Vector3 Target ;

    public float degreesPerSecond;


    void Awake()
    {
    photonView.RPC ("ChangeMyName", PhotonTargets.AllBuffered, PhotonNetwork.playerList.Length.ToString ());
    }

    [PunRPC]

    void ChangeMyName(string myNewName)
    {
    gameObject.transform.name = myNewName;
    }

    void Start ()
    {

    // if ourCamera is null then set the Main Camera to the variable ourCamera
    if (photonView.isMine)
    {
    ourCamera = Camera.main ;
    }


    }

    void Update ()
    {
    if (photonView.isMine)
    {// DrawRay ();
    Vector3 Target = Camera.main.ScreenToWorldPoint (Input.mousePosition);

    Target.z = transform.position.z;
    Quaternion rotation = Quaternion.Euler (new Vector3 (30, 0, 0));
    transform.position = Vector3.MoveTowards (transform.position, Target, Speed * Time.deltaTime / transform.localScale.x);
    }
    }


    NetworkManager Code:
    using System;
    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;

    /// <summary>
    /// This script automatically connects to Photon (using the settings file),
    /// tries to join a random room and creates one if none was found (which is ok).
    /// </summary>
    public class ConnectAndJoinRandom : Photon.MonoBehaviour
    {
    /// <summary>Connect automatically? If false you can set this to true later on or call ConnectUsingSettings in your own scripts.</summary>
    public bool AutoConnect = true;
    public GameObject player;
    [SerializeField]
    private GameObject[] spawnPoints;
    public byte Version = 1;

    /// <summary>if we don't want to connect in Start(), we have to "remember" if we called ConnectUsingSettings()</summary>
    private bool ConnectInUpdate = true;


    public virtual void Start()
    {
    PhotonNetwork.autoJoinLobby = true; // we join randomly. always. no need to join a lobby to get the list of rooms.
    }

    public virtual void Update()
    {
    if (ConnectInUpdate && AutoConnect && !PhotonNetwork.connected)
    {
    Debug.Log("Update() was called by Unity. Scene is loaded. Let's connect to the Photon Master Server. Calling: PhotonNetwork.ConnectUsingSettings();");

    ConnectInUpdate = true;
    PhotonNetwork.ConnectUsingSettings(Version + "0.1" + SceneManagerHelper.ActiveSceneBuildIndex);
    }
    }


    // below, we implement some callbacks of PUN
    // you can find PUN's callbacks in the class PunBehaviour or in enum PhotonNetworkingMessage


    public virtual void OnConnectedToMaster()
    {
    Debug.Log("OnConnectedToMaster() was called by PUN. Now this client is connected and could join a room. Calling: PhotonNetwork.JoinRandomRoom();");
    PhotonNetwork.JoinRandomRoom();
    }

    public virtual void OnJoinedLobby()
    {
    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();");
    PhotonNetwork.JoinRandomRoom();
    }

    public virtual void OnPhotonRandomJoinFailed()
    {
    Debug.Log("OnPhotonRandomJoinFailed() was called by PUN. No random room available, so we create one. Calling: PhotonNetwork.CreateRoom(null, new RoomOptions() {maxPlayers = 4}, null);");
    PhotonNetwork.CreateRoom(null, new RoomOptions() { MaxPlayers = 4 }, null);
    }

    // the following methods are implemented to give you some context. re-implement them as needed.

    public virtual void OnFailedToConnectToPhoton(DisconnectCause cause)
    {
    Debug.LogError("Cause: " + cause);
    }

    public void OnJoinedRoom()
    {
    PhotonNetwork.Instantiate (player.transform.name, Vector3.zero, Quaternion.identity, 0);
    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");
    }




    }





    }