- Home /
sending commands from multiple clients
I'm experimenting with HLAPI and I'm facing a simple problem that doesn't seem to be handled by any of the functionality provided by the netwoking system. Let's say I have a simple cube in the scene that starts moving when any client press a button. It seems that I can send a command to the server from a client if the client has the authority over that object, however this page sates: http://docs.unity3d.com/ScriptReference/Networking.NetworkIdentity.AssignClientAuthority.html
Only one client can own an object at any time
Let's say the players have simply to cooperate based on client inputs of a central copy of the object on the server (the case of the cube), how can one make remote objects receive commands from multiple clients?
code example:
[Command]
void CmdToggleMoving(){
moving = !moving;
logger.ShowMsg("Moving: " + moving);
}
void checkObjectClick(){
if(Input.GetMouseButtonDown(0)){
RaycastHit hit;
Ray ray;
if(settings.usingVufuia)
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
else
ray = settings.selectedDebugCamera.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit)){
// Debug.Log("hit something " + hit.transform.name);
if(hit.transform.name == this.transform.name){
CmdToggleMoving();
logger.ShowMsg("Moving: " + moving);
}
}
}
Answer by Teravisor · Jan 31, 2016 at 10:22 PM
Make several separate invisible objects for each player which they own; make a command on them that moves a cube; now each client sends command to his(her) own object causing cube(same for both) to move.
I get the point of this, but to me it seems highly impratical because I should derive the object behaviour and assign it to something other (that a player owns). $$anonymous$$eaning this separate object should have the reference of the cube, but lets say we have 10 or 20 cubes and each one has a different behaviour, what should I do then? reference each one of them?
why I can't just assign ownership of a object to multiple players?
Answer by OndrejP · Nov 20, 2017 at 07:20 AM
Add NetworkBehaviour script to player object. This script will contain the command method. On server it will find the cube (which can be also passed as command argument) and control it. This way multiple players can move it at the same time.
This will work, because each client is invoking command on the player-owned object (the player).
Your answer
Follow this Question
Related Questions
UNET Different size of spawned object on host and client 1 Answer
Unet Networking: having two clients in the editor instead of building for standalone? 0 Answers
UNet Portal 2 Like Object Interaction Client to Server 1 Answer
[UNET] Only spawn certain server objects on local client? 0 Answers
How to watch in client applications what it is happening in the server one? 0 Answers