In a Multyplayer game how should synch gameObject physics?
Hello folks,
I'm trying to create a multiplayer experience where one player send every 30FPS, only if the objects are moving or the expiration time of 5s is passed, the information for all the GameObjects in the scene to all the other players. For the networking system, ( sending packet and receiving) I am using Oculus Integration platform sdk.
void Update()
{
if (IsServer)
{
timer += Time.deltaTime;
if (timer > TIME_TO_CHECK_S) // send packat only every 0.033 s -> 30FPS
{
var acceleration = (rigidbody.velocity - previousVelocity) / timer;
var angularAcceleration = (rigidbody.angularVelocity - previousAngularVelocity) / timer;
var isAccelerating = (acceleration - previousAcceleration).magnitude > 0.1f ||
(angularAcceleration - previousAngularAcceleration).magnitude > 0.1f;
var hasMoved = (rigidbody.position != previousPosition || rigidbody.rotation != previousRotation) &&
(rigidbody.velocity == Vector3.zero && rigidbody.angularVelocity == Vector3.zero);
var replicationTimeExpired = (DateTime.Now - lastReplicationEventTime).TotalSeconds > FORCE_REPLICATION_TIME_MS;
// the object is forced to be replicated if:
// - its acceleration has changed
// - it has moved (without accelerating)
// - it has not been replicated for 5000 ms
if (isAccelerating || hasMoved || replicationTimeExpired)
{
ReplicateSelfToAll();
lastReplicationEventTime = DateTime.Now;
}
previousVelocity = rigidbody.velocity;
previousAngularVelocity = rigidbody.angularVelocity;
previousAcceleration = acceleration;
previousAngularAcceleration = angularAcceleration;
timer -= TIME_TO_CHECK_S; // Subtracting two is more accurate over time than resetting to zero.
//Debug.Log("Sending");
}
}
previousPosition = rigidbody.position;
previousRotation = rigidbody.rotation;
}
The problem that I am experiencing is that when I am not the server If I throw a ball, there is a really small time, when it goes ahead then suddenly behind and at the end follows the correct path.
[WHAT I HAVE ALREADY TRIED]
When I receive the packet I blend the information received with the one received before, but the result are terrible: when throwing a ball, it goes ahead then it goes behind, then it disappear and at the end it appears where it would have landed if the physics was executed normally.
Answer by salvolannister · Jul 03, 2020 at 03:24 PM
This is still not a perfect solution but makes you go somewhere
I tried to make some modifications reading the suggestions reported in this post and now when I receive a packet I do these checks:
public void ManageRemoteUpdate(ObjectUpdatePacket update)
{
// updating rigidbody.position is faster than updating transform
// even if computative more expensive
// do an approximation
Vector3 newPosition = update.position.ToVector3();
Vector3 difference = newPosition - rigidbody.position;
float distance = difference.magnitude;
Quaternion newRotation = update.rotation.ToQuaternion();
// the dot product gives the cosine between the two angles two parallel vector will have 1
float angleDifference = Vector3.Dot(transform.forward, difference); // [-1,1]
if (angleDifference < 0) //90 degrees
{
rigidbody.rotation = update.rotation.ToQuaternion();
Debug.Log(" angles are really different for " + name);
}
else if(angleDifference < 0.5f)
{
rigidbody.rotation = Quaternion.Lerp(rigidbody.rotation, newRotation, 0.4f); // interpolates the rotation between the two
}
if (distance > 2.0f)
rigidbody.position = update.position.ToVector3();
else if (distance > 0.1)
rigidbody.position += difference * 0.1f;
// needs to be updated istantly
rigidbody.velocity = update.velocity.ToVector3();
rigidbody.angularVelocity = update.angularVelocity.ToVector3();
}
With this solution everything is smother I can't see the ball coming back to my hands for few frames but I still have a little glitch when I throw a cube.
Your answer
Follow this Question
Related Questions
Set Main Camera in Networkmanager 1 Answer
Unet Game on Phton Server? 0 Answers
Boat controller help 0 Answers