Search Unity

Third Party Mirror networking - Player Rubberbanding movement

Discussion in 'Multiplayer' started by Groone, Feb 17, 2019.

  1. Groone

    Groone

    Joined:
    Mar 17, 2018
    Posts:
    7
    I'm looking for some much appreciated help from a kind expert.

    So I am learning and trying to make a networked space game but I am stuck with player movement synchronizing properly along the clients. I am using Mirror asset for my networking. I'm just not sure why the spaceships are rubber banding as they are in the attached video. Anyhow, thank you very much for your assistance.



    Here is the code I am using
    Code (CSharp):
    1. public class PlayerController : NetworkBehaviour
    2. {
    3.     Vector3 addedForce = Vector3.zero;
    4.     float forwardThrust = 300f;
    5.     float maxSpeed = 15.0f;
    6.     float rotationSpeed = 150f;
    7.     Rigidbody2D thisPlayer;
    8.  
    9.     private float updateInterval;
    10.  
    11.     [SyncVar]
    12.     Vector3 realPosition = Vector3.zero;
    13.  
    14.     [SyncVar]
    15.     Quaternion realRotation;
    16.  
    17.     [SyncVar]
    18.     Vector3 realVelocity = Vector3.zero;
    19.  
    20.     private void Start()
    21.     {
    22.         thisPlayer = gameObject.GetComponent<Rigidbody2D>();
    23.     }
    24.     void Update()
    25.     {
    26.         if (isLocalPlayer)
    27.         {
    28.             CmdSync(thisPlayer.position, transform.rotation, thisPlayer.velocity);
    29.             ShipControl();
    30.         }
    31.         else
    32.         {
    33.             thisPlayer.position = Vector3.Lerp(thisPlayer.position, realPosition, 0.0f);
    34.             thisPlayer.velocity = Vector3.Lerp(thisPlayer.velocity, realVelocity, 0.0f);
    35.             transform.rotation = Quaternion.Lerp(transform.rotation, realRotation, 0.0f);
    36.         }
    37.     }
    38.  
    39.     private void ShipControl()
    40.     {
    41.         if (Input.GetKey(KeyCode.UpArrow))
    42.         {
    43.             thisPlayer.freezeRotation = true;
    44.             addedForce = transform.up * forwardThrust * Time.deltaTime;
    45.             thisPlayer.AddForce(addedForce);
    46.             if (thisPlayer.velocity.magnitude > maxSpeed)
    47.             {
    48.                 thisPlayer.velocity = thisPlayer.velocity.normalized * maxSpeed;
    49.             }
    50.             thisPlayer.freezeRotation = false;
    51.         }
    52.  
    53.         if (Input.GetKey(KeyCode.LeftArrow))
    54.         {
    55.             thisPlayer.freezeRotation = true;
    56.             transform.Rotate(Vector3.forward, rotationSpeed * Time.deltaTime);
    57.             thisPlayer.freezeRotation = false;
    58.         }
    59.  
    60.         if (Input.GetKey(KeyCode.RightArrow))
    61.         {
    62.             thisPlayer.freezeRotation = true;
    63.             transform.Rotate(Vector3.back, rotationSpeed * Time.deltaTime);
    64.             thisPlayer.freezeRotation = false;
    65.         }
    66.     }
    67.  
    68.     [Command]
    69.     void CmdSync(Vector3 position, Quaternion rotation, Vector3 velocity)
    70.     {
    71.         realPosition = position;
    72.         realVelocity = velocity;
    73.         realRotation = rotation;
    74.     }
    75.  
    76. }
    Here is how the ship looks in the inspector
     
    Last edited: Feb 18, 2019
  2. Groone

    Groone

    Joined:
    Mar 17, 2018
    Posts:
    7
    After some work, I was able to fix my issue. I ended up stripping all the lerp, networking components from the abvove script and created a new script that seems to work really well. This is the code I ended up with.

    Code (CSharp):
    1. public class PlayerSyncPosition : NetworkBehaviour
    2. {
    3.     [SyncVar]
    4.     private Vector2 syncPos;
    5.  
    6.     [SyncVar]
    7.     private Quaternion syncRot;
    8.  
    9.     [SerializeField] Transform myTransform;
    10.  
    11.     void FixedUpdate()
    12.     {
    13.         TransmitPosition();
    14.         LerpPosition();
    15.     }
    16.  
    17.     void LerpPosition()
    18.     {
    19.         if (!isLocalPlayer)
    20.         {
    21.             myTransform.position = syncPos;
    22.             myTransform.rotation = syncRot;
    23.         }
    24.     }
    25.  
    26.     [Command]
    27.     void CmdProvidePositionToServer(Vector2 pos, Quaternion rot)
    28.     {
    29.         syncPos = pos;
    30.         syncRot = rot;
    31.     }
    32.  
    33.     [ClientCallback]
    34.     void TransmitPosition()
    35.     {
    36.         if (isLocalPlayer)
    37.         {
    38.             CmdProvidePositionToServer(myTransform.position, myTransform.rotation);
    39.         }
    40.     }
    41. }
    Code (CSharp):
    1. public class PlayerController : NetworkBehaviour
    2. {
    3.     Vector3 addedForce = Vector3.zero;
    4.     float forwardThrust = 300f;
    5.     float maxSpeed = 15.0f;
    6.     float rotationSpeed = 150f;
    7.     Rigidbody2D thisPlayer;
    8.  
    9.     private void Start()
    10.     {
    11.         thisPlayer = gameObject.GetComponent<Rigidbody2D>();
    12.     }
    13.     void Update()
    14.     {
    15.         if (!isLocalPlayer) return;
    16.         ShipControl();
    17.     }
    18.  
    19.     private void ShipControl()
    20.     {
    21.         if (Input.GetKey(KeyCode.UpArrow))
    22.         {
    23.             thisPlayer.freezeRotation = true;
    24.             addedForce = transform.up * forwardThrust * Time.deltaTime;
    25.             thisPlayer.AddForce(addedForce);
    26.             if (thisPlayer.velocity.magnitude > maxSpeed)
    27.             {
    28.                 thisPlayer.velocity = thisPlayer.velocity.normalized * maxSpeed;
    29.             }
    30.             thisPlayer.freezeRotation = false;
    31.         }
    32.  
    33.         if (Input.GetKey(KeyCode.LeftArrow))
    34.         {
    35.             thisPlayer.freezeRotation = true;
    36.             transform.Rotate(Vector3.forward, rotationSpeed * Time.deltaTime);
    37.             thisPlayer.freezeRotation = false;
    38.         }
    39.  
    40.         if (Input.GetKey(KeyCode.RightArrow))
    41.         {
    42.             thisPlayer.freezeRotation = true;
    43.             transform.Rotate(Vector3.back, rotationSpeed * Time.deltaTime);
    44.             thisPlayer.freezeRotation = false;
    45.         }
    46.     }
    47. }
     
    Eisnel likes this.
  3. joasus12345

    joasus12345

    Joined:
    Dec 26, 2018
    Posts:
    14
    Wouldn't there be jittery movements when you assign the transform position and rotation directly like that?
     
  4. gamerninjask

    gamerninjask

    Joined:
    Mar 5, 2020
    Posts:
    2


    thanks for your code with your code i finishly can make my own movement on your principe