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