[英]Can't execute server command from client
I'm trying to spawn a bullet when player hit screen. 我试图在玩家击中屏幕时产生一颗子弹。 When I make server in matchmaking I can spawn bullets and I can see them on client side too.
当我在配对中制作服务器时,我可以生成子弹,我也可以在客户端看到它们。 But when I connect like client to server on matchmaking I can't spawn bullets.
但是当我在匹配时将客户端连接到服务器时,我无法生成子弹。 I got this error on server side:
我在服务器端遇到此错误:
Found no behaviour for incoming [Command:InvokeCmdCmd_Fire] on Player(Clone) >(UnityEngine.GameObject), the server and client should have the same NetworkBehaviour instances [netId=2].
在Player(Clone)>(UnityEngine.GameObject)上找不到传入[Command:InvokeCmdCmd_Fire]的行为,服务器和客户端应具有相同的NetworkBehaviour实例[netId = 2]。
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
My code: 我的代码:
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
}
I have this script on my player prefab. 我的播放器预制件上有这个脚本。 I have add on player and on bullet prefab Network Identity and Network Transform.
我添加了播放器和子弹预制网络身份和网络转换。 On player I checked Local Player Authority.
在玩家我检查了本地玩家权限。
I also tried to make a new project where I put everything from the official Unity Multiplayer Networking tutorial but it didn't work either. 我还尝试创建一个新项目,我从官方的Unity多人网络教程中放入了所有内容,但它也没有用。
Thanks for your help. 谢谢你的帮助。
I figured it out. 我想到了。 I was destroying this script if it wasn't attached to local player.
如果它没有附加到本地播放器,我正在销毁这个脚本。 I should only do-
我应该只做 -
if(!isLocalPlayer)
return;`
not 不
if(!isLocalPlayer)
{
Destroy(this);
return;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.