Greetings,
A Network View allows to synchronize Transforms and Rigidbodies components, when defined as observed elements.
If I were to observe a script instead, I would have to serialize informations.
In the case of a Transform component, it would be :
function OnSerializeNetworkView (stream : BitStream, info : NetworkMessageInfo)
{
var position : Vector3;
var rotation : Quaternion;
if (stream.isWriting)
{
position = transform.position;
rotation = transform.rotation;
stream.Serialize (position);
stream.Serialize (rotation);
}
else
{
stream.Serialize (position);
stream.Serialize (rotation);
transform.position = position;
transform.rotation = rotation;
}
}
But it is not enough when the gameobject has a rigidbody. From the point of view of other users, the non owners, the gameobject, when moving, can shake a lot.
So, I tried to retrieve the position and rotation of the rigidbody instead.
function OnSerializeNetworkView (stream : BitStream, info : NetworkMessageInfo)
{
var position : Vector3;
var rotation : Quaternion;
if (stream.isWriting)
{
position = rigidbody.position;
rotation = rigidbody.rotation;
stream.Serialize (position);
stream.Serialize (rotation);
}
else
{
stream.Serialize (position);
stream.Serialize (rotation);
rigidbody.position = position;
rigidbody.rotation = rotation;
}
}
But nothing changed.
I added the velocity and angularVelocity…
function OnSerializeNetworkView (stream : BitStream, info : NetworkMessageInfo)
{
var position : Vector3;
var rotation : Quaternion;
if (stream.isWriting)
{
position = rigidbody.position;
rotation = rigidbody.rotation;
velocity = rigidbody.velocity;
angularVelocity = rigidbody.angularVelocity;
stream.Serialize (position);
stream.Serialize (rotation);
stream.Serialize (velocity);
stream.Serialize (angularVelocity);
}
else
{
stream.Serialize (position);
stream.Serialize (rotation);
stream.Serialize (velocity);
stream.Serialize (angularVelocity);
rigidbody.position = position;
rigidbody.rotation = rotation;
rigidbody.velocity = velocity;
rigidbody.angularVelocity = angularVelocity;
}
}
It was ok then.
In order to observe rigidbodies via a script, are velocity and angularVelocity the only variables required ? I removed position and rotation, it seems ok too…