- Home /
UNET: Client Host syncing problems
In my current project I have a little tank set up. Basically it is set up like this.
-Root
-Cockpit
-Canon
- Base
The root has the Network Transform
with a rigidbody and if tested all tanks move smoothly in both Hosts and clients. The problem lies with the children.
I have tried using the Network Transform Child
for each part, as each can move freely. However, I have found out they do not sync so smoothly as the NetworkTransform with the rigidbody. So I have been investigating and found a fix. I tried 2 ways implementing this fix, i'll explain both.
First Attempt
Here I tried to create my own script
[SyncVar]
private float m_SyncRotation;
[SerializeField]
private float m_LerpRate = 10;
[SerializeField]
private float m_RotThreshhold = 5;
[SerializeField]
private Transform m_Transform;
private float m_LastRot;
private void Update()
{
LerpRotations();
}
private void FixedUpdate()
{
TransmitRotations();
}
void LerpRotations() {
if (!isLocalPlayer)
{
Vector3 newRotation = new Vector3(m_SyncRotation, 0f, 0f);
m_Transform.localRotation = Quaternion.Lerp(m_Transform.rotation, Quaternion.Euler(newRotation), m_LerpRate * Time.deltaTime);
}
}
[Client]
void TransmitRotations()
{
if (isLocalPlayer)
{
if (Mathf.Abs(m_Transform.localEulerAngles.magnitude - m_LastRot) > m_RotThreshhold)
{
m_LastRot = m_Transform.localEulerAngles.x;
Cmd_ProvideRotationToServer(m_LastRot);
}
}
}
[Command (channel = Channels.DefaultUnreliable)]
private void Cmd_ProvideRotationToServer(float rotation)
{
m_SyncRotation = rotation;
}
private Transform FindChild(Transform trans, string tag)
{
foreach (Transform child in trans)
{
if (child.CompareTag(tag))
{
return child;
}
Transform searchedChild = FindChild(child, tag);
if (searchedChild)
{
return searchedChild;
}
}
return null;
}
It is relatively simple however, even if the objects synced smoothly, on the Host machine the second and third instances of this script went crazy and started to synchronize the wrong children but if the host moved the tank the client saw the synchronization correct. I.E. I had 3 instances of this script on the root object, and assigned the base, cockpit and canon to the transform variable respectively. On the client I saw the host tank moving just fine, on the host only the base synced correctly the rest it seemed took the rotation of the base also.
Since this didn't quite work and I knew the Network Transform Child
did work just laggy.
Second Attempt
In this attempt I just took the entire Network Transform Child
script and made just the necessary adjustments(class name change and everywhere they used Network Transform Child
I changed it to my class) since i literally didn't change anything except that I thought it had to work since I used the exact same layout with my script and the original Network Transform Child
.
It didn't go as planned, sadly. Again on the client I saw the hosts movement correctly but now on the host I saw the clients base rotating but all other parts didn't move at all.
IMO this doesn't even make sense, but if anyone could help me out on this I would appreciate it very much.