- Home /
How to select a specific network player - photon unity networking
Hi guys. This is my first post at answers.unity3d. Please excuse if i have posted this in the wrong section.
I have been working on unity for a few months now and have a basic understanding of the environment. Currently i am working on a multiplayer game and for that i am using Photon Unity Networking utility ( PUN intended :P ). This would be my first multiplayer game.
I have a room and a corresponding scene where all the players join and can roam around freely. What i want to do is to be able to touch/click on a player and start a battle with just that player ( I have the code for selecting objects using raycast and colliders but what that does is give me the object that i created locally using photon.instantiate to mimic the network players. )
The way i have decided on implementing this is to take myself and the target player to a different room ( battle room ) and load a new scene ( battle scene ) while the other players are still in the free roaming room / scene.
QUESTIONS:
(1) How can i select a specific network player and communicate with just that player to tell them that a match is about to start between that player and myself and make them join a different room with me?
(2) Does it make a difference if i load the battle scene before or after i join the battle room?
(3) How can i make the target player load the new scene and check over the network if their device has finished loading the new scene?
I AM a Photon illiterate ( maybe even a Unity illiterate ) so please excuse if i annoy you.
I shall appreciate all answers.
Hello @garrodtheif , I have same situation , I have multiple player walking in the scene . now i have to touch on one player and then join in a room for battle between two players , and declare the winner of battle . Could you help me out how can i achieve it. Thanks
Answer by garrodtheif · Jan 13, 2017 at 08:56 AM
This is the script i have in my level where i have to choose an opponent:
if (Input.touchCount >= 1) //Check for touch
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
touchStartPos = touch.position;
}
if (touch.phase == TouchPhase.Ended)
{
if (touch.position == touchStartPos) //Ensure that it is a touch and not a swipe
{
Ray cursorRay = Camera.main.ScreenPointToRay(touch.position);
RaycastHit hit;
if (Physics.Raycast(cursorRay, out hit, 1000.0f))
{
if (hit.transform.gameObject.tag == "opponent") //Tag of opponent
{
selectedOpponent = hit.transform.gameObject;
myObject = GameObject.FindGameObjectWithTag("MyObject");
string roomName = "" + myObject.gameObject.transform.name + selectedOpponent.gameObject.transform.name + (int) Random.Range(1,100) ;
myObject.GetComponent<PhotonView>().RPC("StartFight", PhotonPlayer.Find(int.Parse(selectedOpponent.transform.name)), roomName);
myObject.GetComponent<PhotonView>().RPC("StartFight", PhotonPlayer.Find(int.Parse(myObject.transform.name)), roomName);
}
}
}
}
}
This is the RPC in my network script, the script in which my communication streams are sending and receiving information:
[PunRPC]
void StartFight(string roomName)//pID = local player's viewID, oID = target player's viewID
{
PhotonNetwork.LeaveRoom();
PlayerPrefs.SetString("roomName", roomName);
SceneManager.LoadScene(Constants.fightSceneName);
}
Answer by satyagames · Jul 14, 2016 at 12:33 PM
You need to maintain the player Information at Database.
When you click the particular player - you need to retrive the player information from userInfo table to store it into war table. Now you can able to communicate that player easily… When PhotonNetwork.playerList.Length is 2 .. at your room… then you should send a massage through DB ( php and mysql etc.. )
You can do entire process at battle scene…. then you load your characters…. i mean after creating the room.
When PhotonNetwork.playerList.Length is 2 … the room is full… then you remove your UI panel.
Hi @satyagames.
Thanks for your response.
Now you can able to communicate that player easily… When PhotonNetwork.playerList.Length is 2 .. at your room… then you should send a massage through DB ( php and mysql etc.. )
I have not implemented any sql or php database with my game. I have used PhotonNetwork.SetPlayerCustomProperties to save viewID in a hashtable and then upon touch, i retrieved the values and used the following check to specify target players:
[PunRPC]
void StartFight( string pID , string oID , string roomName ) //pID = local player's viewID, oID = target player's viewID
{
foreach ( PhotonPlayer player in PhotonNetwork.playerList )
{
if (player.customProperties.ContainsValue(pID) )
{
PhotonNetwork.LeaveRoom(); // THIS IS WHERE I LEAVE THE CURRENT ROO$$anonymous$$ AND IN THE COROUTINE I use PhotonNetwork.JoinOrCreateRoom(roomName, roomOptions, TypedLobby.Default) to start/join a new room for the battle
PlayerPrefs.SetString("roomName", roomName);
StartCoroutine(newRoom(roomName));
}
else if ( player.customProperties.ContainsValue(oID) )
{
PhotonNetwork.LeaveRoom();
StartCoroutine(newRoom(roomName));
}
}
}
I am experiencing 2 issues with this code:
(1) When this RPC call is sent using myObject.GetComponent<PhotonView>().RPC("StartFight", PhotonTargets.All, myObject.GetComponent<PhotonView>().viewID.ToString(), selectedOpponent.GetComponent<PhotonView>().viewID.ToString(), roomName);
It is supposed to check viewIDs and only make two players leave the room but it makes all the players leave the room.
(2) When it tries to Create or join a new room it give the following error:
JoinOrCreateRoom failed. Client is not on $$anonymous$$aster Server or not yet ready to call operations. Wait for callback: OnJoinedLobby or OnConnectedTo$$anonymous$$aster.
I thought this should let me communicate with just the target player but it is not behaving as i thought it would.
I can't figure out where the problem lies.
Any help is appreciated.
Your answer
Follow this Question
Related Questions
UNET Networking: Changing Players texture 0 Answers
What to do when user clicks back on google play games realtime multiplayer auto-match ui ? 0 Answers
Online Mulitplayer options in Free Unity 1 Answer
Photon network won't join random room with a custom property 0 Answers
PUN - Choose random Hero for player 0 Answers