嘿,我是Unity3D网络的新手!
当我生成武器(预制件)时,我想向所有客户通知其变换设置(位置,旋转,父级等等),以便它以正确的值附加到玩家。
问题是只有主持人知道所有更改,而对于其他客户端,只有本地播放器(himslef)会更新。
我不确定我是否正确使用了Command和ClientRpc属性^^' 这是代码:
void Start()
{
if (isLocalPlayer)
CmdSpawnGun();
}
[Command]
void CmdSpawnGun()
{
GameObject gunPref = Resources.Load("Prefabs/GunPrefab") as GameObject;
if (gunPref == null)
Debug.Log("Failed to load Gun Prefab.");
GameObject gun = Instantiate(gunPref, _weaponSlot);
gun.GetComponent<Weapon>().Equip(this);
NetworkServer.Spawn(gun);
RpcNotifyTransform(gun, gameObject);
}
[ClientRpc]
void RpcNotifyTransform(GameObject block, GameObject rootIdentity)
{
Transform slot = rootIdentity.GetComponent<Character>()._weaponSlot;
if (slot == null)
Debug.Log("Weapon slot not found.");
block.transform.parent = slot;
block.transform.localPosition = new Vector3(0.0f, 0.0f, 0.0f);
block.transform.localRotation = new Quaternion(0.0f, 0.0f, 0.0f, 0.0f);
block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
}