How to move an object over the network. If only the client controlls/moves that non-player object?
Hello all, I need some help with my code to move an object over the network. in the code i have, the object that i want to move moves on the network and on the clients. This is done by use a command and a RPC call to a syncvar (Vector3) on my movingobject. The strange thing is that that same syncvar is set to the right value i want with the command and RPC but then it also go back to the old value again. So the object goes back to its old position. (not wat i want). i cant find it where that is happening that the syncvar is going back to its old position. so i want a object that can move and its getting controlled by a client. I think i do something wrong in the code that i have and i hope someone can help me with wat i am doing wrong here.
This is the script on the player object for the client:
public Vector3 newPosition;
public GameObject gameObjectTodrag;
void Awake()
{
gameObjectTodrag= GameObject.FindGameObjectWithTag("movingObject");
newPosition = gameObjectTodrag.transform.position;
}
void Update()
{
if (isClient && isLocalPlayer)
{
MyMove();
}
}
void MyMove()
{
CmdMoveTouchOnServer(gameObjectTodrag, movingObjectsWithPosition[gameObjectTodrag]);
}
[Command]
void CmdMoveTouchOnServer(GameObject obj, Vector3 newPos)
{
objNetId = obj.GetComponent<NetworkIdentity>();
if (!hasAuthority)
{
objNetId.AssignClientAuthority(connectionToClient);
}
RpcMoveTouchOnServer(obj, newPos);
}
[ClientRpc]
void RpcMoveTouchOnServer(GameObject obj, Vector3 newPos)
{
obj.GetComponent<MoveObject>().newPosition = newPos;
}
On the MoveObject script it will handle the movement to look at the "newPosition" defined like this:
[SyncVar]
public Vector3 newPosition;
Answer by dazapa2894 · May 19, 2016 at 03:57 PM
do you have NetworkTransform component attached to the object you want to move?
highly recommend you this tutorial:
look at the 7 point: NETWORKING PLAYER MOVEMENT https://unity3d.com/es/learn/tutorials/topics/multiplayer-networking/networking-player-movement?playlist=29690
Thanks for the information/tutorials. yes i have the networkTransfom components on the objects.
i have found the solution also: the mistake was here
if (isClient && isLocalPlayer)
{
$$anonymous$$y$$anonymous$$ove();
}
This is on both clients true. so if 1 client change the value the other one changed it back. since i work with 1 pc and 1 tablet i can make it work on only 1 client to check on what device its running. i also found that the RPC calls are not needed if you work with syncVar and that you can do obj.GetComponent<$$anonymous$$oveObject>().newPosition = newPos;
in the command.
Your answer
Follow this Question
Related Questions
When Do SyncLists get Initialized? 0 Answers
Synchronize scene loading networking 1 Answer
Establishing a local connection to server 1 Answer
Teleporting a NetworkTransform 0 Answers