Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

help with authorative server

Discussion in 'Multiplayer' started by INSANE_BR, Sep 25, 2009.

  1. INSANE_BR

    INSANE_BR

    Joined:
    Sep 10, 2009
    Posts:
    142
    Hi there, Im trying to make a car game with an authorative server. The server is a client also.

    The way I was trying to do was:

    1 - All clients send user inputs via RPC to the server.
    2 - The server apply all the physics to all cars.
    3 - The server send back a RPC with the new position, rotation and some other important data.

    It was working pretty good as my computer as server.
    Both cars were moving on server and clients.

    But when another player created the game, the client's car wont move for the client! but it move on the server.

    I will post the code here:

    Code (csharp):
    1.  
    2. function Update() : void{
    3.  
    4.     if(userControled){
    5.         if(Network.isClient){
    6.             networkView.RPC("GetUserInput",RPCMode.Server,Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), Input.GetAxis("Jump"));
    7.         }
    8.         else if(Network.isServer){
    9.             GetUserInput(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), Input.GetAxis("Jump"));
    10.         }
    11.     }
    12.  
    13. // DO INTERPOLATION / EXTRAPOLATION HERE
    14. }
    15.  
    16. @RPC
    17. function GetUserInput(pVertical : float, pHorizontal : float, pJump : float){
    18.  
    19. // DO PHYSICS HERE
    20.  
    21. networkView.RPC("GetClientState",RPCMode.Others, rigidbody.position, rigidbody.velocity, rigidbody.rotation, rigidbody.angularVelocity, parseFloat(Network.time) , FrontLeftWheel.steerAngle);
    22.  
    23. }
    24.  
    25. @RPC
    26. function GetClientState(pPos : Vector3, pVel : Vector3, pRot : Quaternion, pAngVel : Vector3, pTimeStamp : float ,pSteerAngle : float){
    27.  // ADD STATE TO STATE LIST
    28. }
    29.  
    All interpolation and extrapolation are based on Unity examples. I use a 20 state list.

    I didnt write the physics and interpolation code here because the code would be too big for one post only.. hehe

    Well, Im not sure this is the best way to make an Authorative server (I couldn't make it work with a network view observing a interpolation code) and I have no clue why it wont work changing servers.

    Thanks in advance,
     
  2. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    Hey,

    I couldn't see anything wrong with your code. The error might be somewhere in your interpolation code or the scene-setup. I can't tell.

    Just a few things that came to mind when I saw your code:

    I would recommend you separate the interpolation-script from the input-script. Just use the standard NetworkRigidbody script from the examples. Then you know that the problem isn't with the interpolation / interpretation of the network-data.

    Also sending RPCs every frame probably isn't a good idea. You might get 100 RPCs per second. And most of them share the same information (in a car game at least)!

    Here is an example project of a very simplistic authorative server that works exactly as you described above.

    I put the input-handling in a coroutine. This lets me control how many RPCs regarding input are sent to the server.

    Let me know if it works for you.

    Cheers
     
  3. INSANE_BR

    INSANE_BR

    Joined:
    Sep 10, 2009
    Posts:
    142
    Hmm I still cant understand where I'm going wrong.

    I tried to put the input RPC call inside a coroutine sending it every 0.1 secs, but it got TOO laggy and I'm trying new values.

    But anyway, the problem still persist. If I create a server in Unity API and run a browser or Standalone App the game run smooth and everything goes as expected.

    But when a friend of mine create a server and I connect via LAN, the client can move. I mean, it moves at the server, but it seems the server doesn't send an RPC back to the client...

    And strangest, if we change who's server and client, the game works fine!

    Damn... :(
     
  4. INSANE_BR

    INSANE_BR

    Joined:
    Sep 10, 2009
    Posts:
    142
    Hi all, somehow I could make it work...

    I have no clue why, but if I add some kind of prediction, the client respond as expected.

    the prediction I've added was actually commenting the 'else if' at
    Code (csharp):
    1.  
    2. else if(Network.isServer){
    3.          GetUserInput(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), Input.GetAxis("Jump"));
    4.       }
    5.  
    I really wanna know why this is working now... :(
     
  5. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    The clients now move because you move them locally.
    However this means that the movement on the server and on the client are 2 different actions. This is not a fix!
    If you play long enough differences between the client and server world will appear, that will not be corrected by your code!

    I'm guessing that you are not sending Position Updates from the server to the clients for the client-controlled objects. I can't tell however by what you've posted here.
    Give a little more information or send me your project folder (only the necessary stuff), then I'll take a look.
     
  6. INSANE_BR

    INSANE_BR

    Joined:
    Sep 10, 2009
    Posts:
    142
    Hmmm It actually worked, the server was correcting the position and everything was smooth.

    Its all instanble. Once it was working all nice and smooth and out of the blue it stopped..

    Now if I open 1 client and 1 server in my PC and 2 clients in a friends PC, all my instances work fine, but my friend's can't see any player moving except him (auth server but with prediction)...
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    if he does not see anyone then I would guess your creation call is incorrect.

    Either you tried network.instantiate or RPC.xxxBuffered from the client (these must be server called or won't be buffered for later connecting players as clients are not entitled to buffer stuff for other players, only the server is) or you have some anomaly in the instantiate replication code
     
  8. INSANE_BR

    INSANE_BR

    Joined:
    Sep 10, 2009
    Posts:
    142
    Hmmmm I think you got it.. My clients were calling Network.Instantiate. I will try to call it from the server and hope it works. hehe

    :)
     
  9. INSANE_BR

    INSANE_BR

    Joined:
    Sep 10, 2009
    Posts:
    142
    In fact they see each other, but for clients, no one moves (except themselves)

    Yes the clients were calling network.instantiate. Now the server is (I confirmed the server owns all objects) and the problem persist :(

    Sometimes its VERY VERY laggy, and sometimes the clients seems to be in another world, just moving themselves.

    Any clue?
     
  10. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    1) is the network component attached to the object set to reliable?
    2) is the network view id setup done automatically or are you messing with it?
    3) do the objects that are instantiated on the client for other player have the corresponding setup with respect to the input handling components and the network handling components (remote objects commonly are handled quite differently as you don't want the player to be server controlled as it is the case for the remote object. nothing is worse than 250ms - 500ms latency between input and reaction. you only let the server correct stuff thats impossible commonly)
     
  11. INSANE_BR

    INSANE_BR

    Joined:
    Sep 10, 2009
    Posts:
    142
    Hey dreamora, I actually gave up for now. I was trying to use only RPC calls, and now I'm using state synchronization, but answering your questions:

    It was set OFF, cause I was not using state synchronization.

    Automatically. Remember, the server recives all RPC calls from clients, and for the server the game run smooth.

    Yes. All clients have a camera set to follow he client's car and I could see the cameras were following the proper cars. As I put some prediction code, the client could move his car even before the response from the server (that actually, never came...)

    For now I will use state synchronization and I hope some day I understand what I'm missing. That must be something very noobish that I'm not used to yet.

    Thanks for all the help :)[/quote]