我正在尝试使用 Unity Netcode for Gameobjects 制作 3D 射击游戏。 在主机上,一切正常,但在客户端上,子弹预制件在错误的位置生成。
我尝试遵循教程和阅读文档,但没有一个有帮助。我从旧的完美工作项目中复制了旧网络代码版本的代码,但确切的代码在我当前的项目中无法正常工作。我当前生成子弹的代码是
[ServerRpc]
void ShootServerRpc()
{
GameObject bullet = Instantiate(bulletPrefab, firePoints[0].position, firePoints[0].rotation);
bullet.GetComponent<NetworkObject>().SpawnWithOwnership(OwnerClientId);
ShootClientRpc(bullet.GetComponent<NetworkObject>().NetworkObjectId);
}
[ClientRpc]
void ShootClientRpc(ulong _id)
{
NetworkObject netObj = NetworkManager.Singleton.SpawnManager.SpawnedObjects[_id];
netObj.GetComponent<Rigidbody>().AddForce(netObj.transform.forward * bulletSpeed, ForceMode.Impulse);
}
我还尝试传递位置和旋转参数而不是使用 firePoints,但这并没有改变任何东西。 我究竟做错了什么?谢谢
经过一周的搜索、询问和更改代码,我终于解决了这个错误,从
2022.3.3f1
切换到 2021.3.16f1
。无需更改任何其他内容即可解决该错误。我相信新版本的 Unity 有问题。