- Home /
Interpolation of player's position over RPC calls
Hi guys!
So I'm trying to establish my first game that utilizes the Network, so please bear with me :p
I'm trying to set a Server with full authority. The clients, in order to make movements, pass the information of motion unto the server, who then communicates the changes made, towards the clients.
I think I have done this correctly, but now I have run into the problem of jittering. I have tried somethings to make the jitter less prominent through modification of the timestep, send rate and assigning function to the LateUpdate.
But I guess that there is no way to make the movement smooth if I don't do interpolation.
So I guess the question is this>
If I'm using the following RPC to move the player (In this case a RotateAround), how could I do interpolation?
Is this an incorect view to update the players position? Is there a simpler way
I dont really understand all the things that are already written on the internet, :((
Any help much appreciated :D, Its hard being self taught : /
var target : Transform;
var haveControl : boolean = false;
function Update()
{
if(haveControl)
{
if(Network.isClient)
{
var MouseY : float = Input.GetAxis("Mouse X");
networkView.RPC("MoveMouseToServer", RPCMode.Server, MouseY);
}
}
}
function LateUpdate () {
if (Network.isServer)
{
networkView.RPC("updatePlayer", RPCMode.OthersBuffered, transform.position);
networkView.RPC("updatePlayerRot", RPCMode.OthersBuffered, transform.rotation);
}
}
@RPC
function MoveMouseToServer(MouseY : float){
transform.RotateAround(target.transform.position, Vector3.up, MouseY*3);
}
@RPC
function updatePlayer(playerPos : Vector3){
transform.position = playerPos;
}
@RPC
function updatePlayerRot(playerRot : Quaternion){
transform.rotation = playerRot;
}
Any Ideas?
Is there a way to do something like Lerp, the actual position, with the information that is being received from the server when they do>
networkView.RPC("updatePlayer", RPCMode.OthersBuffered, transform.position);
networkView.RPC("updatePlayerRot", RPCMode.OthersBuffered, transform.rotation);
Is there a way to set a variable with the information that is being received via RPC?
Answer by jpthek9 · Jan 13, 2015 at 09:45 PM
Have a variable LastReceive representing the last time you got the positional update then a LastReceiveDelta representing the last time it took to get another positional update. Also have the LastPosition which is the last positional update you're interpolating from and store the new positional update in NewPosition. Then...
void Update()
{
transform.position = Vector3.lerp(LastPosition, NewPosition,
(Time.time - LastReceive) / LastReceieveDelta);
}
[RPC] PositionalUpdate(Vector3 UpdatedPos)
{
LastPosition = NewPosition;
NewPosition = UpdatedPos;
LastReceiveDelta = Time.time - LastReceive;
LastReceieve = Time.time;
}
Where should I call this?
[RPC] PositionalUpdate(Vector3 UpdatedPos)
I'd say PositionalUpdate is equal to UpdatePlayer in your script. Just use @RPC ins$$anonymous$$d of [RPC]
Hey! You set me on the right track! thanks! I got a smooth movement when I apply this scripts into objects, but for some reason, it is not the case when I add it to a camera, any idea why this is the case?
Are you setting the camera's position the exact same way?
Here is my working code, for the use of the people, implemented into a WASD movemnt (and not a Rotate Around). I changed some of the math because it was causing Nan and negative values. Perhaps due to differencesin the execution order and between the prodecuals calls?
#pragma strict
//Unit relatedd components and characteristics
var director : Transform;
var haveControl : boolean = false;
//Speed $$anonymous$$anagement
var val : float = .5;
var speed : int = 1;
var JumpStrenght : float = 600;
//Positional Update in the server
var LastPosition : Vector3;
var NewPosition : Vector3;
var LastReceived : float;
var LastReceivedDelta : float;
var IntervalNet : float;
var LastReceivedRot : float;
var LastReceivedDeltaRot : float;
var IntervalNetRot : float;
var LastRotation : Quaternion;
var NewRotation : Quaternion;
var ServerPosition : Vector3;
var ServerRotation : Quaternion;
function Update(){
if(Network.isServer){
ServerPosition = transform.position;
ServerRotation = transform.rotation;
networkView.RPC("PositionalUpdate", RPC$$anonymous$$ode.AllBuffered, ServerPosition);
networkView.RPC("PositionalUpdateRot", RPC$$anonymous$$ode.AllBuffered, ServerRotation);
}
if(haveControl)
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
{networkView.RPC("$$anonymous$$oveSphereDF", RPC$$anonymous$$ode.Server, val);}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.S))
{networkView.RPC("$$anonymous$$oveSphereDF1", RPC$$anonymous$$ode.Server, val);}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
{networkView.RPC("$$anonymous$$oveSphereDR", RPC$$anonymous$$ode.Server, val);}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
{networkView.RPC("$$anonymous$$oveSphereDR1", RPC$$anonymous$$ode.Server, val);}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{networkView.RPC("JumpSphere", RPC$$anonymous$$ode.Server, val);}
}
if (Network.isClient)
{
if(!System.Single.IsNaN(IntervalNet)){
transform.position = Vector3.Lerp(LastPosition, NewPosition, LastReceivedDelta);
}
if(!System.Single.IsNaN(IntervalNetRot)){
transform.rotation = Quaternion.Lerp(LastRotation, NewRotation, LastReceivedDeltaRot);
}
}
}
@RPC
function PositionalUpdate(ServerPosition : Vector3){
LastPosition = NewPosition;
NewPosition = ServerPosition;
LastReceivedDelta = Time.time - LastReceived;
LastReceived = Time.time;
//transform.position = Vector3.Lerp(LastPosition, NewPosition, LastReceivedDelta);
}
Answer by murkertrer · Jan 16, 2015 at 02:43 PM
You were right! It was not. My camera had the same script, but it was a child of an object, who had itelf to deal with RPC calls!
Thanks for the help buddy :D
Your answer
Follow this Question
Related Questions
Rotation via RPC and wrong result angle 1 Answer
How To Deal With Lingering Prefabs in Multiplayer Scene ? 0 Answers
Which is better: NetworkView or RPC? 1 Answer
RPC method gets called before Start(). 0 Answers
UNET Rotation of Child transform 1 Answer