Hi everyone !
I’m struggling with an issue now for quite some days, and I just can’t seem to get to a good solution ![]()
My setup is: I have some Networked objects of which I want the connecting clients to be able to move their positions. I also want those objects to have their last set positions to be persistent in the world, so that when new cients connect, the position of the objects will be initialized to the last-set position of the objects by any previous client. I managed to do this by using syncvars and spawning the objects on the server by using Spawn(). As in the documentation it reads that any networked object spawned by the server, will keep its syncvariables stored correctly, so that any new client connecting will see this latest syncvariable.
My problem now is that whenever I move an object, it will set the syncvar on the server-instance of that object and that change will propagate to ALL clients, including the client that was changing the position. You can probably guess what the problem now is: I change the position of an object (change it locally first so there is no delay) than it gets updated on the server with a [command] method which sets the syncvar. And sure enough the syncVar position gets updated, and so the position will reset its position by the newly updated syncvar.
I want to prevent this on the client changing the object’s position, A.K.A. the authorative client ! I know similar situations are done by using a isLocalPlayer check , to prevent the localplayer from updating, but this is different, as it is not the player-object but an other object of which the local player might be authoritative of. I am also aware of the hasAuthority check, but that’s not working out either. since there is a delay going from the client to the server and back, and authoriazation of the client for that particular object might already be gone.
Wondering if other people ran into the same issue and what can be done for it. Any help MUCH appreciated!
Here is some code to clarify:
---------------------------- the dragable object ----------------------------
using UnityEngine;
using UnityEngine.Networking;
public class ObjectSyncTransform : NetworkBehaviour
{
[SyncVar]
public Vector3 syncPos;
[SerializeField]
private float lerpRate = 15;
[SerializeField]
private float translationTreshold = 0.1f;
void Update()
{
LerpTransform();
}
[ClientCallback]
void LerpTransform()
{
//ifit’sthelocalplayermovingwedon’tneedtoupdatetothevaluecomingfromtheserver(it’salreadyupdatedlocally)
if (!hasAuthority)
{
if (Vector3.Distance (transform.position, syncPos) > translationTreshold) {
transform.position = Vector3.Lerp (transform.position, syncPos, ((Time.deltaTime * lerpRate) > 1.0 || (Time.deltaTime * lerpRate) < 0.0) ? 0.5f : Time.deltaTime * lerpRate);
}
}
}
}
---------------------------- the raycast "dragging function on the client ----------------------------
[Command]
void Cmd_moveObjectToPosition(GameObject obj, Vector3 pos)
{
NetworkIdentity objId = obj.GetComponent();
objId.AssignClientAuthority (connectionToClient);
obj.GetComponent().syncPos = pos;
objId.RemoveClientAuthority (connectionToClient);
}