Search Unity

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

Question Rigidbody not being referenced (Unity Netcode)

Discussion in 'Netcode for GameObjects' started by AngelHYYPE09, Nov 22, 2023.

  1. AngelHYYPE09

    AngelHYYPE09

    Joined:
    Jan 31, 2022
    Posts:
    15
    Sorry for coming up with two questions in 4 days, but something strange is happening again on my game
    So, my game is basically a "Pizza restaurant co-op game" where you can obviously make pizzas and deliver them. You can spawn and pick the toppings to make the pizza, and for picking the objects, the game uses a raycast to hit and detect the rigidbodies so it can disable the topping's gravity and move it as the player wants to.
    Everything works great, the player can spawn the objects, but when he tries to pick them up, the rigidbody does not appear on the variable, meaning it's not being referenced. The function does get called, as i recieve that "grab" message on the console, but the variable just doesn't get the rigidbody and for that reason i can't grab the object.
    These are the lines where the player launch the raycast and gets the rigidbody:
    Code (CSharp):
    1.  // LAUNCH RAYCAST
    2.         Ray CamRay = playerCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    3.         if (Physics.Raycast(CamRay, out RaycastHit HitInfo, PickRange, PickMask))
    4.         {
    5.             CurrentObj = HitInfo.rigidbody;
    6.             CurrentObj.useGravity = false;
    7.             Debug.Log("grab");
    8.         }
    Btw, i'm also using ClientNetworkTransform and the NetworkRigidbody component in all the objects i want to spawn and grab.
    I think it is very important to mention that the code provided there and also the code for spawn the toppics are all in an ServerRpc function. I don't know if i can't do that in ClientNetworkTransform because it's Owner Authoritive...i think...
     
  2. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    437
    Hi @AngelHYYPE09 , have you already had a look at the examples in the official ClientDriven sample? (Download here)

    It show how to pick objects up in a multiplayer environment, maybe you'll find an example to follow.

    Does this help?
     
  3. AngelHYYPE09

    AngelHYYPE09

    Joined:
    Jan 31, 2022
    Posts:
    15
    Hi, so i saw that and it does help since now i get the rigidbody that the client wants to grab, but it just doesn't move.
    I don't know what to do at this point, it's kind of confusing because everything works fine but the object doesn't change it's position at any moment, while the host can do that.
     
  4. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    437
    If the host can move the object but other clients can't, it means that either you're managing the object in a server-authoritative way or other clients don't have ownership over it.
     
  5. AngelHYYPE09

    AngelHYYPE09

    Joined:
    Jan 31, 2022
    Posts:
    15
    So i tried to spawn the objects with ownership, but i get an error saying "The name 'clientId' does not exist in the current context". Do i need to do something else in my code before using clientId?

    I used this line:
     Mynetobj.GetComponent<NetworkObject>().SpawnWithOwnership(clientId); 
     
  6. AngelHYYPE09

    AngelHYYPE09

    Joined:
    Jan 31, 2022
    Posts:
    15
    Hey so i just solved it, for anyone in my situation, just create a function with a serverRpc and a ulong in the object script you want to pick, like this:
     public void GiveOwnershipServerRpc (ulong clientId) 


    Inside that function, put this:
     GetComponent<NetworkObject>().ChangeOwnership(clientId); 


    And in your player script, when you want to pick an object, do something like this:
    Code (CSharp):
    1. // Grab object
    2.  
    3. public void GrabFunction()
    4. {
    5.     // Launch raycast
    6.     Ray CamRay = playerCamera.ViewportPointToRay(new
    7.     Vector3(0.5f, 0.5f, 0f));
    8.  
    9.     // if raycast hits an object...
    10.  
    11.     if (Physics.Raycast(CamRay, out RaycastHit HitInfo,
    12.     PickRange, PickMask))
    13.       {
    14.           HitInfo.collider.gameObject.GetComponent<ObjectScript>().GiveOwnershipServerRpc(NetworkManager.Singleton.LocalClientId);
    15.  
    16.       }
    17.  
    18. }

    I'm not sure if this implementation is correct, but it did work for me so i hope it works for someone! Thanks for your help!
     
  7. AngelHYYPE09

    AngelHYYPE09

    Joined:
    Jan 31, 2022
    Posts:
    15
    With that being said, i have another problem, it's with the spawn of the objects, so i have a ServerRpc function that spawns the ingredients depending on what machine the player interacts with, almost all the machines work, but the last one, which is for the dough dispenser, doesn't work, even if it has almost the exact same code! Maybe it's some bug of the netcode or im doing something wrong, which is probably the case since i am pretty new in this stuff. Here's my code:

    Code (CSharp):
    1.     [ServerRpc(RequireOwnership = false)]
    2.     private void AllgrabServerRpc()
    3.     {
    4.  
    5.         Ray CamRay = playerCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    6.         Debug.Log("does this?");
    7.  
    8.         if (Physics.Raycast(CamRay, out RaycastHit HitInfo_pepperoni, PickRange, PickMask_Pepperoni))
    9.         {
    10.             Pepperonimachinescript.inst.OnPressedPepperoni();
    11.             Transform SpPepper = Instantiate(Pepobj, TargetSpawnPp.transform.position, Pepobj.transform.rotation);
    12.             SpPepper.GetComponent<NetworkObject>().Spawn(true);
    13.         }
    14.  
    15.         if (Physics.Raycast(CamRay, out RaycastHit HitInfo_mushroom, PickRange, PickMask_Mushroom))
    16.         {
    17.             Mushroommachinescript.inst.OnPressedMushroom();
    18.             Transform SpMush = Instantiate(Mushobj, TargetSpawnMush.transform.position, Mushobj.transform.rotation);
    19.             SpMush.GetComponent<NetworkObject>().Spawn(true);
    20.         }
    21.  
    22.         if (Physics.Raycast(CamRay, out RaycastHit HitInfo_blackolive, PickRange, PickMask_Olive))
    23.         {
    24.             Bolivemachinescript.inst.OnPressedOlive();
    25.             Transform SpBlack = Instantiate(Oliveobj, TargetSpawnOlive.transform.position, Oliveobj.transform.rotation);
    26.             SpBlack.GetComponent<NetworkObject>().Spawn(true);
    27.         }
    28.  
    29.         if (Physics.Raycast(CamRay, out RaycastHit HitInfo_pineapple, PickRange, PickMask_Pineapple))
    30.         {
    31.             Pineapplemachinescript.inst.OnPressedPineapple();
    32.             Transform SpPine = Instantiate(Pineobj, TargetSpawnPine.transform.position, Pineobj.transform.rotation);
    33.             SpPine.GetComponent<NetworkObject>().Spawn(true);
    34.         }
    35.  
    36.         if (Physics.Raycast(CamRay, out RaycastHit HitInfo_doughdispense, PickRange, PickMask_Dough))
    37.         {
    38.             DoughDispense.inst.OnPressedDough();
    39.             Transform SpDough = Instantiate(Doughobj, TargetSpawnDough.transform.position, Doughobj.transform.rotation);
    40.             SpDough.GetComponent<NetworkObject>().Spawn(true);
    41.         }
    42.  
    43.  
    44.         //This
    45.  
    46.  
    47.     }
    Sorry if the code is a mess hahah