Network, how to control non player object from client and send its position to the host side?

There are NetworkIdentity and NetworkTransform attached to this objects prefab, also this prefab is registered as spawneble prefab (in Network manager), this prefab is not a player, player is empty prefab.
I need to create the logic when players are able to change positions of objects, with mouse click, and other players should see these changes. There is simple script, that working good only on the host side (if change objects positions all objects changing their positions on the client side, but it is not working if I use client side nothing change on the host side, server side (in other clients)). I don’t understand how to communicate with all Network components properly, I have read a lot of documentations and it didn’t help me to find solution for this problem. I found some about Assign Client Authority but I didn’t understand how use it correctly and I think if some object owned by local player and that player will be dc in that case that object will be destroyed, I need keep it on the field in the case if player will quit or dc.
I will appreciate any help, thank you.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class BoxController : NetworkBehaviour{

[SyncVar]
public bool notInUse;

public bool followCursor;

void Start()
{
notInUse = true;
followCursor = false;

}
void OnMouseDown()
{
if (notInUse)
{
notInUse = false;
followCursor=true;
}

}
void OnMouseUp()
{
notInUse = true;
followCursor = false;
}
void Update()
{
CubeFollowCursor ();
}

void CubeFollowCursor()
{
if (followCursor)
{
Vector3 temp = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 15);
transform.position = new Vector3 ( Camera.main.ScreenToWorldPoint(temp).x, Camera.main.ScreenToWorldPoint(temp).y, 0);

}
}

}

There’s a couple ways you can do this depending on what you really want to achieve.

You could have a client take control over an object, by sending a command to the server on your player object to request authority over the object you want to move. Then once the client has authority they can manipulate the gameobject as if it was a local object. Using this method would prevent other clients from manipulating the gameobject while another client has authority. It also though makes your game more open to hacking if that is a concern (always an issue when a client has authority over an object).

Another way, and the way I’d probably handle it, would be to send commands to the server using the player object that instruct the server to move these other gameobjects. It will be a bit more complicated to set up this kind of system, but it has the advantage of not having to manage what clients have authority over these gameobjects (server always has authority) and being more resistant to hacking, as you can verify client inputs before applying them to the gameobjects to be moved. You’ll have to come up with a solution though for when multiple clients are attempting to manipulate a single gameobject at the same time.

2 Likes

Thank you, it is usefull information, I wanna use the way to send commands to the server, would be nice if you know any good tutorial (link) where Is shown advanced practical side of Unity Networking programming.

Sorry I don’t know. Hopefully someone else will chime in. When I got started there were next to no tutorials outside of just basic stuff and youtube videos just walking through the basic tutorials.