Want to use smartphone as a game controller

Hey Guys!

I wanna make a Android multiplayer game whith these specs:

  • start a server on one device
  • other devices (Clients) can connect to the server device using the same application
  • the server device acts as a shared game Screen
  • the client devices show private information to it’s owner
  • the acceleration Input from client devices control the avatars showed on the server device, so the client devices acting as game controllers with screen

I’m new on Unity, it’s my first Project and I have the follwing problem:

  • starting a server will spawn a background, connecting clients will spawn avatars
  • i just want to show the graphics only on the server device, but at the moment the graphics are shown on the client side too.
  • I thought to solve this with if(Network.isServer){spawn} but same problem.

This is my NetworkManagement Script so far:

using UnityEngine;
using System.Collections;

public class NetworkManagerServer : MonoBehaviour {
	
	public class Plyr{
	}
	public class PlyrMngr{
				
	}
	
	//server stuff
	public GameObject playerPrefab;
	public Transform playerSpawnPoint;
	public GameObject background;
	public Transform backgroundSpawnPoint;
	
	//client and server stuff
	private string remoteIp = "127.0.0.1";
	private bool refreshing;
	string gameName = "psykoAndroidTest";
	private HostData[] hostData = new HostData[0];
	bool useNat = true;
	int posX ;
	int posY ;
	int width;
	int height;
	
	// Use this for initialization
	void Start () {
		posX = Screen.width/20;
		posY = Screen.width/20;
		width = Screen.width/10;
		height = Screen.width/10;
	}
	
	// Update is called once per frame
	void Update () {
		if(refreshing){
			if(MasterServer.PollHostList().Length > 0){
				refreshing = false;
				Debug.Log (MasterServer.PollHostList().Length);
				hostData = MasterServer.PollHostList();
			}
		}
	}
	
	void OnGUI(){
		if(!Network.isClient && !Network.isServer){
			if(GUI.Button(new Rect(posX,posY,width,height),"Start Server")){
				startServer();
			}
			if(GUI.Button(new Rect(posX,posY+height,width,height),"Refresh Hosts")){
				refreshHostList();
			}
			if(hostData.Length > 0){
			for(int i =0; i< hostData.Length; i++){
				if(GUI.Button(new Rect(posX+width,posY/2+(height*i),width,height), hostData*.gameName)){*
  •  				Network.Connect(remoteIp, 25000);*
    
  •  		}		*
    
  •  	}*
    
  •  	}*
    
  •  }*
    
  • }*

  • //server methods*

  • void startServer(){*

  •  	Network.InitializeServer(30, 25000, useNat);*
    
  •  	MasterServer.RegisterHost(gameName,"TestUnityBA","psyko");*
    
  •  }	*
    
  • void OnServerInitialized(){*

  •  Debug.Log("Server initialized and ready");*
    
  •  if(Network.isServer){*
    
  •  	spawnBackground();*
    
  •  }*
    
  • }*

  • void OnMasterServerEvent(MasterServerEvent mse){*

  •  if(mse == MasterServerEvent.RegistrationSucceeded){*
    
  •  Debug.Log("Registered Server");*
    
  •  }*
    
  • }*

  • void OnPlayerConnected(){*

  •  Debug.Log("Player connected");*
    
  •  if(Network.isServer){*
    
  •  	spawnPlayer();*
    
  •  }*
    
  • }*

  • void spawnBackground(){*

  •  Network.Instantiate(background, backgroundSpawnPoint.position, backgroundSpawnPoint.rotation,0);*
    
  • }*

  • void spawnPlayer(){*

  •  Network.Instantiate(playerPrefab, playerSpawnPoint.position, playerSpawnPoint.rotation,0);*
    
  • }*

  • //client methods*

  • void OnConnectedToServer(){*

  •  Debug.Log("yesman");*
    
  • }*

  • void refreshHostList(){*

  •  MasterServer.RequestHostList(gameName);*
    
  •  refreshing = true;*
    
  • }*
    }

So my question is:
How do i show graphics only on one device, in this case the server side?
If my question isn’t specific enough feel free to ask me anything.
Thanks for your time and help!
Phil

You need to load the clients into a different scene than the server. The Server’s scene has all the graphics, and the client’s scene has whatever other functionality the clients might need.

You say you have working move-functionality on your GameOjects. Good, that should stay the same, possibly with slight modifications: because the clients will need to have an object to control, you could consider spawning an empty GameObject (rather, spawning the same thing for both, but stripping everything but the NetworkView from the client’s version) for them. That transform is then synced with the server.

Alternatively, you could attempt to serialize the object-control data (acceleration?) and send that over the network, letting the server handle moving the transform according to that data.

Without knowing more context, it’s hard to come up with more ideas.

Hi Philiomanie,

I’m trying to make a game in a similar style to what you’ve described (1 shared screen, smartphones as controllers).

I’m having some trouble getting networking to run smoothly on Android devices, it works correctly on PC but as soon as I try running the apk on two devices the server side works great but the client seems to be able to control both cubes on both screens.

I was attempting to get basic networking under wraps first before trying to strip out client side code to make the shared experience, would you suggest just trying to do it your way from the beginning instead?

Also, looking at the code you’ve posted, what have you changed in the Unity editor to get this working? i.e which scripts have been applied to which objects? Which gameObjects have the relevant network views?

The camera stuff is completely lost on me so far, from what i’ve read in Jamora’s answer you’ve set up the game to run on both devices but just pointed the client’s camera somewhere else?

Also, pretty cheeky, but I was wondering if you perhaps had an example project uploaded somewhere that you wouldn’t mind sharing?

Hey Phazor,

sry for kept you waiting, I’m pretty busy these days and nights.
Since some days are gone, since i’ve posted my request, there are some changes to my project.

I’ll try to describe my architecture:

  • same application on server device and client device

  • networkManagement script on both devices manages the network stuff: whos is server, who is client (functionality as above)
    [text tutorial for authoritative networking][1] [vid tutorial for networking][2]

  • in the actual game scene there is a gameStateManager Object, with some scripts attached to it, managaing player spawning and player movement

  • the gamestateManagerObject is Network.Instantiated on server side with a network view attached to it (synchro off and none to observe)so it can receive RPC calls from client devices (tilt information in my case)

  • on server side the gameStateManager (locally not network) instantiates a player prefab (the player object which representates the players in game) for each player in the actual network.connections

    foreach(NetworkPlayer player in Network.connections){
    avatar = avatarList*;*
    _ spawnPoint = spawnPointList*;_
    _
    if(Network.isServer){_
    Instantiate(avatar, spawnPoint.position, spawnPoint.rotation);
    _
    }_
    _
    connectPlayerWithAvatarTag.Add (player, avatar.tag);_
    _ avatarList= GameObject.FindWithTag(avatar.tag);
    i++;
    }
    - each player object is linked via hashtable with players ( networkPlayer > tag of object) in my project there are different kind of player prefabs with unique tags attached to them*
    - on client side the tilt information which is collected in a script attached to gameStateManager is sent via RPC to server side
    - gameStateManager on server links received tilt information to the right player prefab
    [RPC]
    void handlePlayerInput(NetworkPlayer player, float x, float y){
    * if(!stopPlayerInput){
    moveDir.x = x;
    moveDir.y = y;
    moveDir.z = 0;
    if (moveDir.sqrMagnitude > 1){
    moveDir.Normalize();
    }
    moveDir=speed;

    avatarTag=connectPlayerWithAvatarTag[player].ToString();
    * foreach(GameObject avatar in avatarList){
    if(avatar.tag.Equals(avatarTag)){
    avatar.rigidbody2D.AddForce(moveDirTime.deltaTime);

    * }
    }
    }
    }
    As you can see, i’m not working with different cameras the way it was mentioned above. My Camera Solution is like this:
    - the actual game action is presented on shared Screen because the presented game objects like players and obstacle are only locally instantiated on server*
    - the smartphone screen only presents information like textures or GUI, which i am only adjusting on client side. the triggers for adjusting them come from server events. therefore i attached a displayManagerScript to the gameStateManager
    - you could have two different cams locally, one on server and one on client
    - you also could have one shared cam and adapt the culling masks in your scripts, so you get different presentations on server and client (if server only show this and this, if client show me that) → [good explanation for manually adjusting layers in script][3]
    Hope that could help you, ask me anything you want to know.
    [1]: http://3dgep.com/?p=4609*_
    _[2]: CG Cookie | Learn Blender, Online Tutorials and Help - CG Cookie | Learn Blender, Online Tutorials and Feedback

    _*[3]: http://akilram.com/using-layermask-in-unity/*_

Same as you did, im trying to develop an Andriod application capable of creating a server/client and share data.

First i tried with UNet, but since im not working with a Unity Pro version, the samples i managed to get were deprecated.

Second approach was AllJoyn’s Unity Plugin, but as you know all the documentation and support for the plugin is deprecated.
I managed to compiled the unitypackage file from version 4.12 but when i try to run a simple scene with nothing but a AllJoynAgent i get an “DllNotFoundException: alljoyn_unity_native”.
I read it was caused by the DLL being compiled with the MonoDevelop default framework 3.5 and not 4.0 as it should. I changed it but nothing happened.

Last but not least, im giving the Google BluetoothChat Sample a look.

Also:

-Guy who did exactly the same but won’t share it

-Guy who did it with the BluetoothChat Sample

.

Thanks for answering and sorry for the discussion misplaced comment