Search Unity

How to sync rigidbody 3d position

Discussion in 'Multiplayer' started by LubDen, May 22, 2017.

  1. LubDen

    LubDen

    Joined:
    Aug 5, 2015
    Posts:
    12
    Hello, I'm creating a game. This game is a mobile balloon throwing game. To move my ball, I use the player's touch screen that I convert to vector3 by adding a force depending on the distance of the touch or the touch time. For the moment the game works very well, but I try to convert it into multiplayer. In multiplayer the ball (rigidbody) has the right movement however it is not synchronized with other clients. I can't do that.
    Can someone help me?
    Thank you for your answers.
     
    Last edited: May 22, 2017
  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Either use the premade NetworkTransform or just write your own. Submit the position x times a second to the server (or their velocity and position). Then from server send a message to the clients to apply the position, rotation and possibly even the velocity to hide the wait time between the messages.
     
  3. LubDen

    LubDen

    Joined:
    Aug 5, 2015
    Posts:
    12
    This is what is tried to do, but not results.


    Code (CSharp):
    1.  if (Vector3.Distance(lastPosition, mouvement) > 0.08f)
    2.             {
    3.                 TransmitPosition();
    4.                 lastPosition = mouvement;
    5.             }
    6.  
    7.  [Command]
    8.     void CmdSendMyPositionToServer(Vector3 positionReceived)
    9.     {
    10.         SyncPostion = positionReceived;
    11.     }
    12.  
    13.     [Client]
    14.     void TransmitPosition()
    15.     {
    16.         CmdSendMyPositionToServer(mouvement);
    17.     }
    18.  
    If is not local player :
    Code (CSharp):
    1.  myTransform.position = Vector3.Lerp(myTransform.position, SyncPostion, Time.deltaTime * 15);
     
  4. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    What happends when you do something like this?
    Code (CSharp):
    1.  
    2. using UnityEngine.Networking;
    3. using UnityEngine;
    4. public class SyncPosition : NetworkBehaviour
    5. {
    6.     private float _lastSendTime;
    7.     public int SendsPerSecond = 10;
    8.    
    9.     void Update()
    10.     {
    11.         if(Time.time - _lastSendTime >= 1f / (float)SendsPerSecond)
    12.         {
    13.             Cmd_SendPos(transform.position);
    14.         }
    15.     }
    16.    
    17.    
    18.     void Cmd_SendPos(Vector3 pos)
    19.     {
    20.         Rpc_ApplyPos(pos);
    21.     }
    22.    
    23.    
    24.     void Rpc_ApplyPos(Vector3 pos)
    25.     {
    26.         transform.position = pos;
    27.     }
    28.    
    29. }
     
    LubDen likes this.
  5. LubDen

    LubDen

    Joined:
    Aug 5, 2015
    Posts:
    12
    Thank you, it works better! The rebounds of the ball are not fair, the rebounds are much bigger than they should be. The balloons are very jerky but I think I can find the solution on the internet.
     
  6. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Yes, the code I provided is very simplistic. It sends the position to the server and applies it on all clients. You should probably do some lerping or even sync the velocity to act as interpolation
     
  7. LubDen

    LubDen

    Joined:
    Aug 5, 2015
    Posts:
    12
    I just finished my interpolation, it works very well, it is very fluid. However when my rigidbody makes rebounds it also makes circles. For now, I don't find the right code on internet or with my tests. I will edit this post when I find the solution.
     
  8. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Finding network code on the internet is very difficult. Write your own. If you elaborate your question I might be able to help you.
    What do you mean with "rebounds"?
    And what do you mean with the rigidbody makes circles?
     
  9. LubDen

    LubDen

    Joined:
    Aug 5, 2015
    Posts:
    12
    For rebounds : When I touch the screen, my ball take power and directions. At the end of the touch, the gravity is active and when the ball touch the ground, she makes a rebound. But with the clients, the ball makes circles betwen two rebounds. I don't know if it's understandable ?!
     
  10. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Send commands to sync those events?
     
  11. LubDen

    LubDen

    Joined:
    Aug 5, 2015
    Posts:
    12
    Do you think, if I sync the rotations it will work?