I recently have been making a inventory system for my game with netcode and struggled for weeks trying to make it feel responsive and stop being so buggy. and i finnaly found the function i needed after alot of google searches and i dont think anyone has every showed this in any tutorial but i think more people are probably struggling with this exact thing.
[Rpc(SendTo.Server, Delivery = RpcDelivery.Reliable)]
public void PickupItemServerRpc(sbyte slot, NetworkObjectReference itemReference, NetworkObjectReference playerReference)
{
if (playerReference.TryGet(out NetworkObject playerNetworkObject))
{
if (itemReference.TryGet(out NetworkObject itemNetworkObject))
{
Inventory inventory = playerNetworkObject.GetComponent<Inventory>();
ItemInfo info = itemNetworkObject.GetComponent<ItemInfo>();
itemNetworkObject.TrySetParent(playerNetworkObject, false);
itemNetworkObject.GetComponent<Rigidbody>().MovePosition(inventory.handPos.position);
info.TogglePickedUpRpc(true);
inventory.CurrentWeight.Value += info.ItemWeight.Value;
PickupItemClientRpc(slot, itemReference, playerReference);
}
}
}
this is my function i had about 20 minutes ago and wandered upon the documentation once again, as setting the rigidbody is making the item freak out and setting the transform behaved even worse in responsiveness.
This is the line in question
itemNetworkObject.GetComponent<Rigidbody>().MovePosition(inventory.handPos.position);
and this is the new line that fixes EVERY inconsistancy
itemNetworkObject.GetComponent<NetworkTransform>().Teleport(inventory.handPos.position, Quaternion.identity, Vector3.one);
the NetworkTransform or ClientNetworkTransform.Teleport function. i know its simple but i just want more people struggling to not suffer like i did. as i have not seen any demonstration of this or even that it exists until i read the documentation.
(which i need to do more often i see)
- sorry for the random rant but i just am happy to finally not suffer, as this solution is working fantastic.