Issues with my network?

Dear Unity users,

I’ve probably got a little around 12-18 hours on unity. I’ve made a game but it was not build on the grounds of multiplayer, I’m trying to add it to be both single and multiplayer with (one map) IE: Single player and multiplayer would be the same map but hold diff data.

Here is my issue:

I’m using the
Photon Unity Networking Free (Free) Edition
I have it all setup and players can join games in session or go in single player and be in offline mode, I have my script on my scene with the map I want players to play, you can choose both single player or multiplayer on the map. I have added network view and photon view to everything in the game, (When I’m logged into the server I can see everything BUT the players that join, they cant see me or hear me shooting. When I move my player they say theirs moves to too or teleports to where mine is. There are vehicals around the map, If I get into one they cant see it move, if they blow it up they cant see it move.

What could be the issue? they can join the server and I added the views onto everything? Is it my end or photons end… I’ve been puzzled and messing around with anything I can for the past 5 hours… I did add the Is Mine to the player in my Hierarchy.

I will try to answer this with my knowledge but I cannot guarantee that it is correct but it ‘should’ work.

When implementing multiplayer into a game its not as easy as adding a photon/network view onto a gameobject.

First of all you said that when a player moves it moves every other player. This is because you need to allow only the owner of the instantiated prefab to control the player.

To solve this you need to use photon views ‘isMine == true’. Add the line below to a script and put an if statement around the lines that involve ownership of an gameobject (I believe you can only get ownership of a instantiated object so instantiate the player when they join).

PhotonView pv = PhotonView.Get(this);
 
 
if(pv.isMine == true)
 
{
 
//Do something here that only the owner can do
 
}
 
else if(pv.isMine != true)
 
{
 
//Do something here that only other players can o
 
}

Secondly you said you couldn’t see player in the scene. I’m curious to know if you instantiate the player over the network and if you start in the game scene then connect to the server or connect to the server then load the game scene.

Assuming you don’t instantiate the player over the network you will need to. Firstly there is a resources folder inside of the photon networking folder in the assets folder. Put the player prefab into the resources folder because that is where photon looks for prefabs to instantiate.

Now you add this to your network script whenever the player joins the room/server.

GameObject player = PhotonNetwork.Instantiate("Put the name of the prefab here!", new Vector3(0, 0, 0), Quaternion.identity, 0);

This will instantiate the prefab ‘Put the name of the prefab here!’ over the network at the Vector3 position 0,0,0. Change the ‘Put the name of the prefab here’ to the name of your player object.

Lastly you can use RPC’s (remote procedural calls) to send data over the network that is usually not constant (like turning a torch on or off instead of sending the position of something). To use an RPC look below.

void Start()
 
{
 
pv.RPC("FunctionToCall", PhotonTargets.AllBuffered, null);
 
}
 
 
[RPC]
 
void FunctionToCall()
 
{
 
     Debug.Log("RPC Sent!");
 
}

Basically an RPC calls a function which is sent over the network. So here whenever the script is started, every player connected to the server and any players that would join will get a debug saying “RPC Sent!” in their console (if they could somehow access the console). Because I set the RPC to AllBuffered, it is sent to itself, every other connected player and players that join later on.
You can also send variables with RPC’s like so.

void Start()
 
{
 
int myInt = 10;
 
 
pv.RPC("SendInt", PhotonTargets.AllBuffered, myInt);
 
}
 
 
[RPC]
 
void FunctionToCall(int receivedInt)
 
{
 
     Debug.Log(receivedInt)
 
}

There are a few more things you should know about networking but I’m still learning it myself so hopefully someone else will help answer your question. I hope this helped!

1 Like

This is my network manager script, I dident have my player camera in my resources, Where exactly do I add "

  • PhotonView pv = PhotonView.Get(this);
    • {
    • //Do something here that only the owner can do
    • }
    • {
    • //Do something here that only other players can o
    • }"

add to any script?

Also, After dragging my player prefab into the resources that photon has I tested multiplayer and I still cannot see the other users,

using UnityEngine;
using System.Collections.Generic;

public class NetworkManager : MonoBehaviour {

SpawnSpot[ ] spawnSpots;

public bool offlineMode = false;

bool connecting = false;

List chatMessages;
int maxChatMessages = 5;

// Use this for initialization
void Start () {
spawnSpots = GameObject.FindObjectsOfType();
PhotonNetwork.player.name = PlayerPrefs.GetString(“Username”, “Awesome Dude”);
chatMessages = new List();
}

void OnDestroy() {
PlayerPrefs.SetString(“Username”, PhotonNetwork.player.name);
}

public void AddChatMessage(string m) {
GetComponent().RPC (“AddChatMessage_RPC”, PhotonTargets.AllBuffered, m);
}

[RPC]
void AddChatMessage_RPC(string m) {
while(chatMessages.Count >= maxChatMessages) {
chatMessages.RemoveAt(0);
}
chatMessages.Add(m);
}

void Connect() {
PhotonNetwork.ConnectUsingSettings( “SNPRE v001” );
}

void OnGUI() {
GUILayout.Label( PhotonNetwork.connectionStateDetailed.ToString() );

if(PhotonNetwork.connected == false && connecting == false ) {
GUILayout.BeginArea( new Rect(0, 0, Screen.width, Screen.height) );
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.BeginVertical();
GUILayout.FlexibleSpace();

GUILayout.BeginHorizontal();
GUILayout.Label("Username: ");
PhotonNetwork.player.name = GUILayout.TextField(PhotonNetwork.player.name);
GUILayout.EndHorizontal();

if( GUILayout.Button(“Single Player”) ) {
connecting = true;
PhotonNetwork.offlineMode = true;
OnJoinedLobby();
}

if( GUILayout.Button(“Multi Player”) ) {
connecting = true;
Connect ();
}
GUILayout.FlexibleSpace();
GUILayout.EndVertical();
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.EndArea();
}

if(PhotonNetwork.connected == true && connecting == false) {
GUILayout.BeginArea( new Rect(0, 0, Screen.width, Screen.height) );
GUILayout.BeginVertical();
GUILayout.FlexibleSpace();

foreach(string msg in chatMessages) {
GUILayout.Label(msg);
}

GUILayout.EndVertical();
GUILayout.EndArea();

}

}

void OnJoinedLobby() {
Debug.Log (“OnJoinedLobby”);
PhotonNetwork.JoinRandomRoom();
}

void OnPhotonRandomJoinFailed() {
Debug.Log (“OnPhotonRandomJoinFailed”);
PhotonNetwork.CreateRoom( null );
}

void OnJoinedRoom() {
Debug.Log (“OnJoinedRoom”);

connecting = false;
GameObject player = PhotonNetwork.Instantiate(“Player”, new Vector3(0, 0, 0), Quaternion.identity, 0);
SpawnMyPlayer();
}

void SpawnMyPlayer() {
AddChatMessage("Spawning player: " + PhotonNetwork.player.name);

if(spawnSpots == null) {
Debug.LogError (“WTF?!?!?”);
return;
}

SpawnSpot mySpawnSpot = spawnSpots[ Random.Range (0, spawnSpots.Length) ];
GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate(“Player”, mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);

//((MonoBehaviour)myPlayerGO.GetComponent(“FPSInputController”)).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent(“MouseLook”)).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent(“PlayerMovement”)).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent(“PlayerShooting”)).enabled = true;
myPlayerGO.transform.FindChild(“Main Camera”).gameObject.SetActive(true);
}
}

I have removed "
((MonoBehaviour)myPlayerGO.GetComponent(“MouseLook”)).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent(“PlayerMovement”)).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent(“PlayerShooting”)).enabled = true;
myPlayerGO.transform.FindChild(“Main Camera”).gameObject.SetActive(true); "
Because it was thought error and game my player multiple arms all over the map (Player) Clone

Now the issue is, When I run server and client on my computer both are on the same map, but when I get into a vehical its like 3rd person the person is in the vehical and I’m still standing outside, If I blow it up both me and the client die…?

As well as the first person view is fine then if a player joins the server the view has extra arms and guns floating around

Here is a video for more detail:
https://drive.google.com/file/d/0Bxnl4eYPav17UlVCaTBSNHNTZE0/edit?usp=sharing