- Home /
Multiplayer Object positions after player rejoin
Hello,
i have the following setup:
As Server i can Spawn objects with "Network.Instantiate(Resources.Load("Items/Food/TestFood"), new Vector3(240.09f, 7.78f, -1633.47f), Quaternion.identity, 0);"
If the player is connected everything is fine, but if the player leaves and rejoins the server, the Instantiated objects are flickering at thier spawn position.
ervy object that has spawned have the following script (and a networkView observing it):
using UnityEngine;
using System.Collections;
public class SimpleNetSync : MonoBehaviour
{
private float lastSynchronizationTime = 0f;
private float syncDelay = 0f;
private float syncTime = 0f;
private Vector3 syncStartPosition = Vector3.zero;
private Vector3 syncEndPosition = Vector3.zero;
private Quaternion syncStartRotation = Quaternion.identity;
private Quaternion syncEndRotation = Quaternion.identity;
private bool gotSync = false;
void Update()
{
if(!networkView.isMine){
SyncedMovement();
}
}
private void SyncedMovement()
{
if(gotSync == true)
{
syncTime += Time.deltaTime;
rigidbody.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
rigidbody.rotation = Quaternion.Slerp(syncStartRotation, syncEndRotation, syncTime / syncDelay);
}
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
Vector3 syncPosition = Vector3.zero;
Quaternion syncRotation = Quaternion.identity;
Vector3 syncVelocity = Vector3.zero;
if (stream.isWriting)
{
syncPosition = rigidbody.position;
stream.Serialize(ref syncPosition);
syncRotation = rigidbody.rotation;
stream.Serialize(ref syncRotation);
syncVelocity = rigidbody.velocity;
stream.Serialize(ref syncVelocity);
}
else
{
stream.Serialize(ref syncPosition);
stream.Serialize(ref syncRotation);
stream.Serialize(ref syncVelocity);
syncTime = 0f;
syncDelay = Time.time - lastSynchronizationTime;
lastSynchronizationTime = Time.time;
syncEndPosition = syncPosition + syncVelocity * syncDelay;
syncStartPosition = rigidbody.position;
syncEndRotation = syncRotation;
syncStartRotation = rigidbody.rotation;
gotSync = true;
}
}
}
all thes objects has a rigidbody for physics.
What can i do to spawn the objects correct for the rejoining player?
I think i need something like "getCurrentDataFormSever()"
edit: I found that if the network view observes the object transform it seams working like expected, but the movement is not smooth.
i think i have a working solution: Some tesing is requierd
using UnityEngine;
using System.Collections;
public class SimpleNetSync : $$anonymous$$onoBehaviour
{
private float lastSynchronizationTime = 0f;
private float syncDelay = 0f;
private float syncTime = 0f;
private Vector3 syncStartPosition = Vector3.zero;
private Vector3 syncEndPosition = Vector3.zero;
private Quaternion syncStartRotation = Quaternion.identity;
private Quaternion syncEndRotation = Quaternion.identity;
private bool gotSync = false;
private bool firstSync = false;
void FixedUpdate()
{
if(!networkView.is$$anonymous$$ine){
Synced$$anonymous$$ovement();
}
}
private void Synced$$anonymous$$ovement()
{
if(gotSync == true)
{
syncTime += Time.deltaTime;
if(firstSync)
{
rigidbody.position = syncStartPosition;
rigidbody.rotation = syncStartRotation;
}
else
{
rigidbody.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
rigidbody.rotation = Quaternion.Slerp(syncStartRotation, syncEndRotation, syncTime / syncDelay);
}
}
}
void OnSerializeNetworkView(BitStream stream, Network$$anonymous$$essageInfo info)
{
Vector3 syncPosition = Vector3.zero;
Quaternion syncRotation = Quaternion.identity;
Vector3 syncVelocity = Vector3.zero;
if (stream.isWriting)
{
syncPosition = rigidbody.position;
stream.Serialize(ref syncPosition);
syncRotation = rigidbody.rotation;
stream.Serialize(ref syncRotation);
syncVelocity = rigidbody.velocity;
stream.Serialize(ref syncVelocity);
}
else
{
stream.Serialize(ref syncPosition);
stream.Serialize(ref syncRotation);
stream.Serialize(ref syncVelocity);
syncTime = 0f;
syncDelay = Time.time - lastSynchronizationTime;
lastSynchronizationTime = Time.time;
if(!gotSync)
{
syncEndPosition = syncPosition;
syncStartPosition = syncPosition;
syncEndRotation = syncRotation;
syncStartRotation = syncRotation;
firstSync = true;
}
else
{
syncEndPosition = syncPosition + syncVelocity * syncDelay;
syncStartPosition = rigidbody.position;
syncEndRotation = syncRotation;
syncStartRotation = rigidbody.rotation;
firstSync = false;
}
gotSync = true;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613184716im_/https://answers.unity.com/themes/thub/images/avi.jpg)