Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Rigidbody Force

Discussion in 'Multiplayer' started by half_voxel, Feb 8, 2009.

  1. half_voxel


    Oct 20, 2007
    It might seam as the simplest thing in the world, but I wonder how I can use forces on a rigidbody from clients. Right now I can only use them on the server, on the clients it will just bump and then go back to the original position (NetworkRigidbody,js). I have tried to add a networkView to the rigidbody and some other stuff but I can't get it working.

    Can someone help?

  2. Der Dude

    Der Dude

    Aug 7, 2006
    Hi there.

    I'll try to give you a few nudges in the right direction. Remember that networking is complex and there are many approaches to one problem.
    Additionally I would recommend that you watch the video about Networking from this site (here).

    Why it is not working:
    The server sends Updates to all clients about this rigidbodys position, rotation etc. The server per se does not receive any information about this object from his clients. So the client locally adds a force to the rigidbody, which moves the object. By the next Update from the server however the object is moved to the position that the server knows.

    You have to notify the server, that the client wants to add a force to this object.
    The quickest to way to implement this would be to add an RPC function to the NetworkRigidbody script, that looks something like this:

    Code (csharp):
    2. [RPC]
    3. public void AddForceRPC( Vector3 force ) {
    5.     //Tell the server to add force to this object
    6.     if( Network.isClient ) {
    7.         networkView.RPC( "AddForceRPC", RPC.Server, force );
    8.     }  else {
    9.         //Add the force ourselves if we are not connected or the server
    10.         rigibody.AddForce( force );
    11.     }
    12. }
    In the code where you want to add force to an object you can then call this function, which will let the server know to add force to this object.


    The state of this object will always be consistent across all instances of the game (Server and clients).


    There will be a slight delay between "bumping" the object and the object actually being moved.
    Say for example you shoot a barrel. On the client you will see the barrel get hit and a fraction of a second later (depending on latency) you will see it react to the impact.

    Depending on your game this may or may not be acceptable.

    Improvement Ideas:

    Client-side prediction. The client can add force to the object instantly and then try to smoothly adopt the position of the barrel on the server.

    This is far more complex though and can lead to strange results.

    Hope I could give you some hints.
  3. half_voxel


    Oct 20, 2007

    Thanks for the answer. I have succeeded to make a script with the same technique as you described and tested it, and it works well :D
    The video was great, I have never bothered to look at those Unite presentations before.