I currently have a networked vehicle in my game that works fine but only for the server. The server is able to drive it without any issues but when a client gets in he can’t control it. I serialiaze the position of the vehicle using this script but it still won’t work unless its the server controlling the vehicle.
Summary: Server can drive the car, Client can’t.
using UnityEngine;
using System.Collections;
public class VehicleSync : MonoBehaviour
{
Vector3 realPosition = Vector3.zero;
Quaternion realRotation = Quaternion.identity;
void Start()
{
if(!gameObject.GetComponent<VehicleMovement>().canControl)
{
rigidbody.isKinematic = false;
}
else
{
rigidbody.isKinematic = true;
}
}
void Update()
{
syncMov();
}
void syncMov()
{
if(networkView.isMine)
{
}
else
{
transform.position = Vector3.Lerp(transform.position, realPosition, 0.1f);
transform.rotation = Quaternion.Lerp(transform.rotation, realRotation, 0.1f);
}
}
public void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
Vector3 thePos = Vector3.zero;
Quaternion theRot = Quaternion.identity;
if(stream.isWriting)
{
//Send our position
thePos = transform.position;
theRot = transform.rotation;
stream.Serialize(ref thePos);
stream.Serialize(ref theRot);
}
else
{
//Receive their postion
stream.Serialize(ref thePos);
stream.Serialize(ref theRot);
realPosition = thePos;
realRotation = theRot;
}
}
}