Search Unity

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

Question MLAPI Rigidbody

Discussion in 'Netcode for GameObjects' started by caiofonseca6, Apr 11, 2021.

  1. caiofonseca6

    caiofonseca6

    Joined:
    Jan 27, 2021
    Posts:
    2
    Hello, I'm trying to create a multiplayer game using MLAPI.

    The game will be like this, the player will be a rolling ball, and it has a rigidbody component. The problem is, whenever I try to syncronize the positions of the players, it doesn't work, unless the player object doesn't have a rigidbody component attached to it.

    What should I do? It seems like NetworkTransform doesn't work with Rigidbodies at all, is there a way to make this work?
     
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    NetworkTransform might not work for dynamic rigidbodies currently, we will have to look into that. You can try something like this NetworkRigidBody script.
     
    Last edited: Jul 16, 2021
    caiofonseca6 likes this.
  3. GaiusAureliusValeriusDiocletianus

    GaiusAureliusValeriusDiocletianus

    Joined:
    May 6, 2021
    Posts:
    2
    +1 here -- I also attempted to naively attach a NetworkTransform to a Rigidbody, and was sad when it didn't work.

    My current workaround is to split the prefab object into "Server" and "Client" prefabs.
    • The client prefab contains only the NetworkObject, NetworkVariables, and MeshRenderers.
    • The server prefab contains the Rigidbody and *Colliders..
    • The NetworkManager spawns only the ClientPrefab, and I separately manage on-server-only instantiation of the ServerPrefab -- I Instantiate() the ServerPrefab during the ConnectionApprovalCallback, after calling the ConnectionApprovedDelegate to Instantiate() and Spawn() the ClientPrefab
    • The ServerPrefab contains a reference to the ClientPrefab(), and updates the NetworkVariables with the results of the physics computations -- similar to how the NetworkRigidBody script works.

    This seems to work OK. The obvious drawback of this is that the client-side contains no Rigidbody simulator, and so the meshes will not move in between updates of the NetworkVariables.
     
  4. wwwwEPwwww

    wwwwEPwwww

    Joined:
    May 23, 2021
    Posts:
    1
    +1 !
    I was also trying to get the rigidbody component of a network object and using it lead to "NullReferenceException: Object reference not set to an instance of an object"...
    Code (CSharp):
    1. NetworkObject ballObject = other.GetComponent<NetworkObject>();
    2.  
    3. //(other comes from OnTriggerEnter)
    4. //later
    5.  
    6. ballObject.GetComponent<Rigidbody>().AddForce(Vector3.forward * racketForce, ForceMode.Impulse);
    My workaround might be to use hard coded serialized field... Tested and seems to work for this purpose where there is only one defined object (ball for a game) and nothing necessarily defined by other methods...

    Code (CSharp):
    1. // The prefab is defined on the object that uses the script
    2. [SerializeField] private Rigidbody BallRigidBody;
    3.  
    4. //And later
    5.  
    6. BallRigidBody.AddForce(Vector3.forward * racketForce, ForceMode.Impulse);
    I'm not sure but this might also be related:
    https://docs-multiplayer.unity3d.com/docs/develop/migration/migratingtomlapi/index.html

    "The NetworkTransform does not have full feature parity with UNET's NetworkTransform. It lacks features like position synchronizing for rigidbodies."
     
    Last edited: May 31, 2021
    Przemo likes this.
  5. Woopy8

    Woopy8

    Joined:
    Apr 4, 2019
    Posts:
    6
    I tried using this script but right at the start where it declares the network variables like this gives me a problem:
    Code (CSharp):
    1. public NetworkVariableVector3 netVelocity = new NetworkVariableVector3(NetworkVariableSettingsUtility.OwnerWrite);
    2.     public NetworkVariableVector3 netAngularVelocity = new NetworkVariableVector3(NetworkVariableSettingsUtility.OwnerWrite);
    3.     public NetworkVariableVector3 netPosition = new NetworkVariableVector3(NetworkVariableSettingsUtility.OwnerWrite);
    4.     public NetworkVariableQuaternion netRotation = new NetworkVariableQuaternion(NetworkVariableSettingsUtility.OwnerWrite);
    5.     public NetworkVariableUInt netUpdateId = new NetworkVariableUInt(NetworkVariableSettingsUtility.OwnerWrite);
    NetworkVariableSettingsUtility isn't something that exists as far as I can tell - and after some googling I can't figure out what on earth it's referring to. Is it part of the wider MLAPI community contributions package? Because I couldn't find it there either.
     
  6. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    I fixed this a while ago. It should no longer be the case on the latest commit on the 'main' branch.
     
  7. Woopy8

    Woopy8

    Joined:
    Apr 4, 2019
    Posts:
    6
    Thanks so much! Managed to get it working now using the latest version.
     
  8. mangax

    mangax

    Joined:
    Jul 17, 2013
    Posts:
    334
    am using version 0.1.0 , and unity 2020.. doesn't seem to find NetworkVariableSettingsUtility
     
  9. CreepyD

    CreepyD

    Joined:
    Jun 9, 2019
    Posts:
    24
    Same issue and version here, can't see any later version to grab.
     
  10. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Did you install the package? if yes, which link did you use?