Networked Vehicle

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;
		}
	}
}

Are you using RPC calls? There doesn’t seem to be anything here that would tell the clients that something happened, and for that matter nore tell the server that something happened.

Only one of the nodes in the network owns the vehicle, and thus only one sends the position / rotation. The rest just receive.

The node that created the object’s NetworkViewID is the owner, and you can check this via networkView.isMine.

You have two options if you want somebody who didn’t create the vehicle to drive it:

  1. Reassign the owner. Have the new owner create a new NetworkViewID and assign it to networkView.viewID. Then send an RPC to everybody else and pass the NetworkViewID so that they also assign the new Id. This has the advantage of being more responsive for the driver.
  2. Ask the server to drive the car. Instead of driving the car directly, send RPCs with commands like ‘steer left’, ‘steer right’, ‘accelerate’, and ‘brake’ to the network node that owns the car. The owner (server, in your case) applies these commands, and the other nodes see the results via the synchronized position and rotation. This has the advantage of allowing multiple clients to drive the same vehicle simultaneously.

Give it a network transform and network identity, and inherit from networkbehaviour instead of monobehaviour.

Also check for the isLocalPlayer value and if it is not then just breakout.
Little late but just for the archives:P