- Home /
The question is answered, right answer was accepted
How to get transform syncing over network to work?
I tried to get a simple change in camera position to work over the network. My goal is that the client can push a UI button to switch the cameras position and rotation. As it is now, it works locally but it doesn't sync the position and rotation over the network. After trying a lot of different things, I figured it would be best to ask the community here.
The code that should handle this is: using UnityEngine; using System.Collections; using UnityEngine.Networking;
public class CameraViews : NetworkBehaviour {
public Transform view1;
public Transform view2;
public Transform view3;
public Transform view4;
[SyncVar] int chosenView = 0;
void Update () {
switch (chosenView) {
case 0:
transform.position = view1.position;
transform.rotation = view1.rotation;
break;
case 1:
transform.position = view2.position;
transform.rotation = view2.rotation;
break;
case 2:
transform.position = view3.position;
transform.rotation = view3.rotation;
break;
case 3:
transform.position = view4.position;
transform.rotation = view4.rotation;
break;
}
}
[Command] public void CmdCamera1 () {
chosenView = 0;
}
[Command] public void CmdCamera2 () {
chosenView = 1;
}
[Command] public void CmdCamera3 () {
chosenView = 2;
}
[Command] public void CmdCamera4 () {
chosenView = 3;
}
}
The camera has a Network Identity component attached as well with local authority turned on and server only turned off.
Hopefully it's an easy fix, but I'm just overlooking it now. Therefore any help is much appreciated.
https://www.youtube.com/watch?v=NLnzlwCRjgc
Start here :) Its a good watch (a bit slow but a good watch).
I followed the tutorial, changed the code to this:
using System.Collections;
public class CameraViews : $$anonymous$$onoBehaviour {
public Transform view1;
public Transform view2;
public Transform view3;
public Transform view4;
public void Camera1 () {
transform.position = view1.position;
transform.rotation = view1.rotation;
}
public void Camera2 () {
transform.position = view2.position;
transform.rotation = view2.rotation;
}
public void Camera3 () {
transform.position = view3.position;
transform.rotation = view3.rotation;
}
public void Camera4 () {
transform.position = view4.position;
transform.rotation = view4.rotation;
}
}
Added a Network Transform to the camera, set up to sync the Transform. But still no succes unfortunately...
Edit: I just found out that when I move the camera through the scene view, it does sync properly with the clients.
Shouldnt your script still derive from NetworkBehaviour, still?
Answer by RoofTurkey · Oct 16, 2015 at 02:25 PM
The solution for me was to make the camera selector client the host, so the full authority over the objects was already there. For more information see the comments on the question.