- Home /
Rotation via RPC and wrong result angle
Hi there,
I have a problem with sending transform rotation using RPC.
Basically, I try to get the mouse input for Y axe when rotating the clone on the client, and rotate the clone on server using this value via RPC.
The rule it's apparently working, but the final rotation angle on the server is a bit (more or less) different from the rotation on client. Why?
Here is the essential part of code I use:
/**
// Transform components
// --------------------
// CharacterMotor.js
// --------------------
var getUserInput : boolean = true; // false on Server, true on Client
var rotateInput : float;
function Update () {
if (getUserInput) {
rotateInput = Input.GetAxis("Mouse X");
}
// rotateInput by RPC on Server, by Input on Client
transform.Rotate(0, rotateInput, 0);
}
// --------------------
// NetworkController.js // active on Client only
// --------------------
var rotateInput : float;
var lastRotateInput : float;
var targetController : CharacterMotor; // (above, assigned)
function Update() {
rotateInput = Input.GetAxisRaw("Mouse X");
if (rotateInput != lastRotateInput) {
Debug.Log("Send rotation: " + rotateInput);
networkView.RPC("SendRotation", RPCMode.Server, rotateInput);
}
lastRotateInput = rotateInput;
}
@RPC
function SendRotation(rotateInput : float) {
Debug.Log("Receive rotation: " + rotateInput);
targetController.rotateInput = rotateInput;
}
Start/Stop rotation on Client. Debug.Log reports looks ok:
Send rotation: 0.2, Receive rotation: 0.2
Send rotation: 0.9, Receive rotation: 0.9
Send rotation: 0.3, Receive rotation: 0.3
Send rotation: 0,25, Receive rotation: 0.25
Send rotation: 0.05, Receive rotation: 0.05
Send rotation: 0, Receive rotation: 0
But:
Final Y angle reported on Client = 25.5
Final Y angle reported on Server = 24.75
What is reason of this difference?
Thanks in advance,
Iulian
Answer by asafsitner · Sep 04, 2012 at 07:00 AM
The difference is probably due to latency.
I believe you'll want to use state synchronization rather than RPC for this, however.
This is not exactly what RPC were meant for.
Thanks for answer, asafsitner. I don't know if I can use state synchronization. I tried to extend TPS-Auth Unity example. The problem with using state synchronization there is that the owner of the script it's server, so that the states sync serialization are send by the server. But the client it's initiating movement. The client must send info. I think this is reason the keyboard inputs (I just added mouse inputs) are send by RPC to the server ins$$anonymous$$d of state sync, then server made the requested transformation and use sync state with corrections goal only. I am missing how to initiate state sync for rotation from the client to the server since client is not owner of the script. On the another hand, I see RPC for positions looks having not a problem with latency.
What I'll do in this case is have a separate object that handles input from the client.
The client will own that object and synchronize keyboard state to the server (I'd personally use a `BitArray`).
The server will move the character on it's end (taking into account latency from the client), and synchronize the position/rotation back to the client. In this case the server owns the character.
The client on it's end smooths the movement received from the server, with correction based on latency as well.
Thanks, asafsitner. I didn't tried syncing using separated object for the inputs. I will try.
Thanks again, asafsitner! Separate objects handling inputs with state syncronization it's working like a charm.
Hey, that's great to hear! Networking can be a real headache sometimes. :)
Your answer
Follow this Question
Related Questions
3D Turret Rotation 0 Answers
How to calculate angles and apply to rotate an object. 1 Answer
Interpolation of player's position over RPC calls 2 Answers
Character not rotating 1 Answer
Get angle from mouse direction? 1 Answer