Android multiplayer using WiFi
Hello everyone, I want to develop an android game which go multiplayer over the wifi (something like "Doodle Army 2: Mini Militia"). I have tried everything which is available on the internet but nothing helped me. I made a game which can go multiplayer on the same system(computer) but when I try it on android devices(making one android device as hotspot and other to connect to it via wifi), it doesn't works. I don't know what is the problem. I am using the following script to handle all of the networking related tasks. Please help! Thank you.
using UnityEngine; using UnityEngine.Networking; using gui = UnityEngine.GUILayout;
public class Network : MonoBehaviour { public GameObject PlayerPrefab; string ip = "127.0.0.1"; private const string typeName = "typename"; private const string gameName = "gamename"; private HostData[] hostList;
public void CreatePlayer()
{
connected = true;
GameObject g = Network.Instantiate(PlayerPrefab, transform.position, Quaternion.identity, 1) as GameObject;
GameObject.Find("PlayerCamera").GetComponent<Camera>().enabled = true;
Destroy(GameObject.Find("MenuCamera"));
}
void OnDisconnectedFromServer()
{
connected = false;
}
void OnPlayerDisconnected(NetworkPlayer pl)
{
Network.DestroyPlayerObjects(pl);
}
void OnConnectedToServer()
{
Debug.Log("Connected to Server");
CreatePlayer();
}
void OnServerInitialized()
{
Debug.Log("Server Initializied");
CreatePlayer();
}
private void RefreshHostList()
{
MasterServer.RequestHostList(typeName);
}
void OnMasterServerEvent(MasterServerEvent msEvent)
{
if (msEvent == MasterServerEvent.HostListReceived)
hostList = MasterServer.PollHostList();
}
bool connected;
void OnGUI()
{
if (!connected)
{
ip = GUI.TextField(new Rect(5, 5, 150, 50), ip, 25);
if (GUI.Button(new Rect(5, 70, 150, 70), "connect"))
{
Network.Connect(hostList[0]); //Both ways are tried but
Network.Connect("127.0.0.1", 25000, ""); //nothing works.
}
GUI.enabled = true;
if (GUI.Button(new Rect(5, 150, 150, 70), "host"))
{
Network.InitializeServer(32, 25000, false);
MasterServer.RegisterHost(typeName, gameName);
Network.sendRate = 15;
}
}
}
}
Heyy, @madhusudan1997.sharma , I just build a project in Unity 5.3 which runs over local multiplayer, (using hotspot), So I have solution to your problem. I see you are working on uNet. I have created my game in uNet too... So do let me know if you still need help . :)
Hii @$$anonymous$$agnas94, I am also making a multiplayer game. can u please help me. i want the devices to be connected using hotspot. Can u please explain the full thing as i am new to unity. Thank you
@RiAmazing @madhusudan1997-sharma @amaanawati86
Listen all, For mobile hotspot the best practice is to take your own IP, and substring it so that the last bit of ip is always .1 , FOR example , if I created hotspot from my device(i.e. if I am host) , then my ip would be something like : "xxx.xxx.xx.1"
But for any client , who is connected to my hotspot , his ID will be something like : "xxx.xxx.xx.a" where a is never 1 but is any random positive integer like 2,3,64,29 etc
so you just substring ur own ip and replace the last part by '1'.
Example : suppoese my ip is: myIP = "xxx.xxx.xx.92" string someAddress = myIP.Substring (0, 10) + ".1";
now this someAdress is what u use to connect to host on hotspot by running :
manager.networkAddress = someAddress;
Hey, m working on same kinda project as yours so I picked up your script and dropped network manager script.But its not working,can you help me a bit with my project. It is to spawn to fabs over lan without internet.
Answer by Bamdad · Aug 29, 2016 at 04:57 AM
You should use 192.168.43.1 for ip. It worked for me on a multiplayer hotspot game.
Sorry, I'm trying to make a LAN connection between two android devices for a game through my WIFI. But it works only with the IP 192.168.43.1, only with an hotspot connection and only when the HOST is the hotspot device...how can I do it works using WIFI?
hey everyone, I'm an absolute beginner. I am learning by making a game that has two modes in which player plays vs Computer and the other mode is multiplayer. 1. Can you people guide me how to do Ai mode along with $$anonymous$$ultiplayer? 2. How online multiplayer in Doodle army like games work? Thanks.
Answer by fredfishgames · Aug 18, 2016 at 08:26 PM
you're trying to make the android device which is the client connect to itself. You need to replace the "127.0.0.1" with the ip address of the host device. I think you can find this somewhere in the wifi settings, but it should be the same for all android devices who are running mobile hotspot.
Sorry if you've tried this already.
Answer by RiAmazing · Jan 30, 2017 at 07:48 AM
@magnas94 I just build a project in Unity 5.3 which runs over local multiplayer, (using hotspot), So I have solution to your problem. I see you are working on uNet. I have created my game in uNet too... So do let me know if you still need help . :)
I also wanted to make the same game, can you help me?
I'm now good with the local and online multiplayer in Unity. If you need any help, just let me know.
bro I am also creating multiplayer game can u tell me how you were able to connect the devices via wifi....
@magnas94 Can you please share the project files .It would be really helpful. I'm also trying to learn on how to make multiplayer games.
any help over a hotspot game would be more helpfull !
Answer by Magnas94 · Feb 01, 2017 at 09:17 AM
@RiAmazing , Its good that you want to work on a similar type of game , so are you using uNet too ? and if you are , then will you be connecting your devices using WiFi, or mobile Hotspot ??