1

Goal: Syncronize player position in a multiplayer game in Unity that uses the Mirror Networking API

Problem:

  1. Using the NetworkTransform component the position seems to not be precisely syncronized, both the client and the server sees the player in the wrong position, players tend to fly with no reason

  2. I tried making a separated script:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;

public class PlayerSyncronization : NetworkBehaviour
{
    [SyncVar]
    public Vector3 syncPosition;
    [SyncVar]
    public Quaternion syncRotation;

    public float lerpRate = 15;

    private void FixedUpdate()
    {
        TransmitPosition();
        Lerp();
    }
    void Lerp()
    {
        if (!hasAuthority) return;
        transform.position = Vector3.Lerp(transform.position, syncPosition, Time.deltaTime * lerpRate);
        transform.rotation = Quaternion.Lerp(transform.rotation, syncRotation, Time.deltaTime * lerpRate);
    }

    [ClientCallback]
    void TransmitPosition()
    {
        CmdProvidePositionToServer(transform.position, transform.rotation);
    }

    [Command]
    void CmdProvidePositionToServer(Vector3 pos, Quaternion rot)
    {
        syncPosition = pos;
        syncRotation = rot;
    }
}

but the result is the same of the network transform component, and even if a player stops walking, on the other side he keeps walking till he falls off the map

Those are the errors/warning i got using the script

Info:

  • Player movement is client side as I'm using root motion.
  • I can't use IsLocalPlayer as it works only if the player is directly spawned by the NetworkManger and my players are being spawned by a separated script.
  • Right now, even if I'm not using it, i have a CharacterController in my PlayerPrefab
  • I'm using Steamworks to make the players connect through internet

If more info are needed just tell me.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
gNt
  • 79
  • 1
  • 9
  • You should also check `if(!hasAuthority) return;` in the `TransmitPosition` .. and in the `Lerp` I'm pretty sure you would want to go the other way round and check `if(hasAuthority) return;` since in that case you want to send not receive – derHugo Jun 07 '21 at 16:45
  • 1
    Also the `CharacterController` like the `Rigidbody` is **physics based** and should **not** be mixed with directly moving something via the `Transform` as it might a) break any physics based behavior and collision detection and b) might interfere with your Networking (see objects continues moving even though stopped on one client -> sounds like the velocity is kept going by the character controller) – derHugo Jun 07 '21 at 16:49
  • @derHugo Thank you, I understood that the problem was the Character controller, so I checked if the player had authority, if not I destroyed it, and if i was destroying it on the server I replaced it with a normal collider without physics – gNt Jun 08 '21 at 15:05

1 Answers1

0

I understood that the problem was the Character controller I was using, so in the player script I checked if the player had authority, if not I destroyed the character controller, and if i was destroying it on the server I replaced it with a normal collider without physics, so i could still have collisions

gNt
  • 79
  • 1
  • 9