【发布时间】:2017-01-24 16:01:30
【问题描述】:
我试图在玩家点击屏幕时生成子弹。当我在配对中创建服务器时,我可以生成子弹,我也可以在客户端看到它们。但是当我在配对时像客户端一样连接到服务器时,我无法生成子弹。我在服务器端收到此错误:
在 Player(Clone) >(UnityEngine.GameObject) 上未发现传入 [Command:InvokeCmdCmd_Fire] 的行为,服务器和客户端应具有相同的 NetworkBehaviour 实例 [netId=2]。
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
我的代码:
for (int x = 0; x < Input.touchCount; x++)
{
touchpos = Camera.main.ScreenToWorldPoint(touch[x].position);
if ((touchpos.x > -4.5f || touchpos.y > -1.2f))
{
pos = transform.position;
if (magazine > 0)
{
if (time > firetime && autoriffle)
{
Cmd_Fire();
time = 0;
magazine--;
}
time += Time.deltaTime;
}
[Command]
void Cmd_Fire()
{
GameObject bullet = Instantiate(bulet, pos, Quaternion.FromToRotation(Vector3.up, new Vector3(touchpos.x, touchpos.y, transform.position.z) - transform.position)) as GameObject;
bullet.GetComponent<Rigidbody>().AddForce(bullet.transform.up * bulletspeed, ForceMode.Impulse);
NetworkServer.Spawn(bullet);
// I try NetworkServer.SpawnWithClientAuthority(bullet, connectionToClient); too but same reason
}
我的播放器预制件上有这个脚本。我添加了播放器和子弹预制网络身份和网络转换。在播放器上,我检查了本地播放器权限。
我还尝试创建一个新项目,将官方 Unity Multiplayer Networking 教程中的所有内容都放入其中,但也没有成功。
感谢您的帮助。
【问题讨论】: