- Home /
Parenting and RPC functions
I have just discovered the power of unity, however the network aspect is still making me scratch my head. I am trying to parent a weapon(equipment) to another invisible gameobject on the players hand(EquipmentSlots).
It works well on my client. However, the other players cannot see the weapon attach to my hand. I am still trying to do more research on RPC function calls, but I believe I am doing this somewhat correct.
In the network view of the object I set the state synchronization to off as told by numerous tutorials.
If anyone could please help to explain to me how to "update" the data of the object(i.e. the parent change and position change) over the network, it would be greatly appreciated!
if(equipment[0])
{
slot = 0;
networkView.RPC("equipGun", RPCMode.AllBuffered, slot);
}
[RPC]
public void equipGun(int slot)
{
if(networkView.isMine)
{
equipment[slot].transform.parent = EquipmentSlots[slot];
equipment[slot].transform.position = new Vector3(EquipmentSlots[slot].position.x,
EquipmentSlots[slot].position.y,
EquipmentSlots[slot].position.z);
equipment[slot].transform.rotation = new Quaternion(EquipmentSlots[slot].rotation.x,
EquipmentSlots[slot].rotation.y,
EquipmentSlots[slot].rotation.z,
EquipmentSlots[slot].rotation.w);
equipment[slot].collider.enabled = false;
}
}
What is the purpose of if(networkView.is$$anonymous$$ine)
?
The RPC will get to the networkView of the same networkViewID that sent the message, meaning it will only be picked up by your clones on the other players's machines. That if statement is going to return true only for each player's local avatar so they never update the data for your clone.
Answer by Dreamside · Mar 17, 2012 at 12:11 PM
What asafsitner said is right. In rpc function you check that if that gameobject is owned by you then equip the item.
Suppose that there are two players in game named A and B. Lets say you
are A. When you call an Rpc it reaches player B. Player B looks that
if it is the owner of object or not via networkView.isMine. But objects' owner is player A so this if statement returns false.
I think you should call rpc with mode RPCMode.others and in body of rpc function don't control for owner information.Because of calling with mode "others" you also should equip gun with a local function.
As a pseudo code i thought something like this:
void EquipGun()
{
// Equip gun codes (will work only locally
// Call RPC function for tell peers to equip gun
}
[RPC]
void RemoteEquipGun()
{
//equip gun
}
That makes a whole lot more sense. This has been wracking in my brain for 2 days now. Just with that little tip, I think I am going to now be able to make dramatic leaps in my game progress.
Thanks man!!