I have vehicles with rigidbodies that send a NetworkTransform update at a rate of 9 updates/second to the other client’s which just updates the remote vehicle’s position/velocity.
What I would like to implement now is ability to send steering/throttle/brake values. I am pretty new to networking but think that I could do it using [clientRPC] at a fixed interval. I was wondering however if I could send the controls data at the same interval (and even in the same packet) as the NetworkTransform data?
Is it possible to attach additional information to a NetworkTransform so it gets updated at exactly the same interval?
-or- Would it be wise to write a custom NetworkTransform?
And are there possibly any other more efficient ways to send NetworkTransforms + steering/throttle/brake values?
Either write a custom NetworkManager, and call a method 9 times a second. Where you send a Command to the server and include Velocity and position like the NetworkManager and you also provide your custom values. Then from that command you Rpc it to all clients.
I ended up using clientRPC and a fixed interval and this works well:
CycleController VehicleControls; //the vehicle controls
void Start () {
VehicleControls = GetComponent<VehicleController> ();
InvokeRepeating("UpdateControls", 5.0f, 0.1f);//controls information (steering, throttle, brakes) to be periodically updated
}
...
//code for setting authority and other network behaviors
...
//If client has authority of the vehicle then send the controls information (steering, throttle, brakes) from this client to the server which in turn will update all clients
void UpdateControls() {
if (hasAuthority) {
CmdUpdateControls (
VehicleControls.Vehicle_SteeringAngle,
VehicleControls.Vehicle_Throttle,
VehicleControls.Vehicle_Brakes
);
}
}
[Command]
public void CmdUpdateControls(float steering, float throttle, float brakes)
{
if (NetworkServer.active) {
RpcUpdateControls (steering, throttle, brakes);
}
}
[ClientRpc]
public void RpcUpdateControls(float steering, float throttle, float brakes)
{
if (VehicleControls) {//only works after Start() has been called on clients
VehicleControls.Vehicle_SteeringAngle = steering;
VehicleControls.Vehicle_Throttle = throttle;
VehicleControls.Vehicle_Brakes = brakes;
}
}
I downloaded that sourcecode and gave it a look, I can see where it would be useful in future projects. Thanks!