- Home /
Problem with moving lights on network
Hi Unity,
I'm having issues with rapidly flickering lights when I run my client on a network.
I have a day/night cycle by using a yellow light and a blue light that rotate around the plane with a simple rotate script.(moon and sun). However, when the client runs it, because it's being updated constantly with my position/rotation update script and network view, the lights flicker a few times every second. I'm guessing this is because there's a constant update of the position of it, but why is this happening? How can I fix this?
If I take the networkView and movement update script off completely, the lighting is smooth. However, it doesn't sync with the other clients (which is very important for this game).
Thanks!!
Movement Script:
using UnityEngine;
using System.Collections;
/*
©2014 Gamer To Game Developer.
This script is attached to an object and it
ensures that the object's position and rotation
are kept up to date across the network.
*/
public class S2_MovementSync : MonoBehaviour {
//Variables Start___________________________________
private Quaternion updatedRotation; //Used in capturing the object's last significant rotation.
private Transform myTransform; //A reference to the object's transform.
private Vector3 updatedPosition = new Vector3();
//Variables End_____________________________________
// Use this for initialization
void Awake ()
{
myTransform = transform;
updatedRotation = myTransform.rotation;
}
// Update is called once per frame
void Update ()
{
if(!networkView.isMine && Network.peerType != NetworkPeerType.Disconnected)
{
myTransform.rotation = Quaternion.Lerp(myTransform.rotation, updatedRotation, Time.deltaTime*10);
myTransform.position = Vector3.Lerp(myTransform.position, updatedPosition, Time.deltaTime*12);
}
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
Quaternion rot = new Quaternion();
Vector3 pos = new Vector3();
if(stream.isWriting)
{
pos = myTransform.position;
stream.Serialize(ref pos);
rot = myTransform.rotation;
stream.Serialize(ref rot);
}
else
{
stream.Serialize(ref pos);
updatedPosition = pos;
stream.Serialize(ref rot);
updatedRotation = rot;
}
}
}
Answer by mediumal · Sep 03, 2014 at 03:38 PM
Your code looks okay. I suspect the problem is that the clients are also running the code you haven't shown that moves the lights over time, resulting in a tug-of-war. Make sure only the server / offline client runs that code via (false == Network.isClient).
Hi, I think you're on the right track here. I had all of my clients moving (technically rotating) the lights as well as the server. I updated my rotation code to try and resolve this per your suggestion. How does this look? It doesn't seem to work for me, though the flickering is less intense now.
using UnityEngine;
using System.Collections;
public class Rotate : $$anonymous$$onoBehaviour {
public float x;
public float y;
public float z;
void Start () {
if (Network.isClient == false) {
enabled = true;
} else {
enabled = false;
}
}
public void RotateLeft()
{
if (!Network.isClient) {
transform.localRotation = Quaternion.Slerp (transform.localRotation, transform.localRotation * Quaternion.Euler (x, y, z), 4f * Time.deltaTime);
}
}
void Update () {
RotateLeft();
}
}