Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by garrodtheif · Jul 14, 2016 at 08:55 AM · unity 5multiplayerunity5multiplayer-networkingmultiplayer networking

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image arjun_singh_2013 · Jan 11, 2017 at 09:28 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by garrodtheif · Jan 13, 2017 at 08:56 AM

@arjun_singh_2013

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);
 }



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image garrodtheif · Jul 15, 2016 at 07:00 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges