1
\$\begingroup\$

I'm having some trouble trying to run my game for the multiplayer part. I'm creating a 2D game. I have followed this tutorial : http://www.paladinstudios.com/2013/07/10/how-to-create-an-online-multiplayer-game-with-unity/ and I've implemented it. As the tutorial says, I have a network view pointing to a script, containing my OnSerializingNetworkView and his interpolation and predilection stuff. It's working very fine, the movement is fluid and it's very good. The problem is when a second player join the game and my first player have already moved, since I'm instantiating the player using Network.Instantiate(prefab, Vector2.zero, ...) the player spawn in the middle of the map and then slowly move to his normal position. I've tried other things such as not using a script but putting the transform in the network view, when the second player spawn, the first one is at the correct position but the mouvements aren't the same and aren't that fluid.

Anyone have any idea on how to deal with this problem ?

Thanks!

\$\endgroup\$
2
\$\begingroup\$

According to your tutorial the fluid movement issue should be dealt with the smooth movement paragraph, which state the following:

private float lastSynchronizationTime = 0f;
private float syncDelay = 0f;
private float syncTime = 0f;
private Vector3 syncStartPosition = Vector3.zero;
private Vector3 syncEndPosition = Vector3.zero;

void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
    Vector3 syncPosition = Vector3.zero;
    if (stream.isWriting)
    {
        syncPosition = rigidbody.position;
        stream.Serialize(ref syncPosition);
    }
    else
    {
        stream.Serialize(ref syncPosition);

        syncTime = 0f;
        syncDelay = Time.time - lastSynchronizationTime;
        lastSynchronizationTime = Time.time;

        syncStartPosition = rigidbody.position;
        syncEndPosition = syncPosition;
    }
}

and

void Update()
{
    if (networkView.isMine)
    {
        InputMovement();
    }
    else
    {
        SyncedMovement();
    }
}

private void SyncedMovement()
{
    syncTime += Time.deltaTime;
    rigidbody.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
}

I once did the same tutorial and the problem was solved as I understood that part.

In a few words you have to Lerp between your client pos and real position on the server.

Hope this could helo you.

EDIT: by the way after trying the stuff in this tutorial I tried different ways to implement networking for my game. I tried the PhotonServer from exitgames which has a very complete api to handle bot cloud and autoritative server. And then I tried and chosen uLink from MuchDifferent, which has built in unity compatibility and fit my needs to the best.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.