【unet】请问客户端如何使用Command
unity3d吧
全部回复
仅看楼主
level 1
场景是按键射出子弹,代码逻辑是通过Command调用ClientRpc,rpc里处理子弹生成及移动。
从服务器端发射是没有问题的,各端都是同步的。
但是从客户端点击,就报Trying to send command for object without authority,客户端的物体是通过NetworkManager自动生成的,请问要怎样才能为客户端的这个物体授权?或者还有更好的做法?
相关代码:
2019年11月01日 02点11分 1
level 1
void Update () {
Move();
if (Input.GetButtonDown("Fire1"))
{
CmdFire();
}
}
[Command]
void CmdFire()
{
if (!isLocalPlayer)
return;
RpcBulletControl();
}
[ClientRpc]
void RpcBulletControl()
{
GameObject bullet = GameObjectType.Bullet.GetGameObject(1);
bullet.transform.position = gun_position.position;
bullet.transform.rotation = gun_position.rotation;
bullet.GetComponent<Rigidbody>().velocity = gun_position.transform.forward * 25;
}
2019年11月01日 02点11分 3
level 4
同问。。。 。。。
2019年11月24日 14点11分 4
level 1
需要将生成的预制在networkmanager中spawn里面注册一下
或者通过代码注册一下
2019年12月25日 07点12分 6
是的, //将生成的子弹同步到各个客户端 NetworkServer.Spawn(bullet);
2021年11月04日 09点11分
level 1
//在调用CmdFire();之前就判断是不是本地,报Trying to send command for object without authority是Host端的本地玩家在客户端所派生出玩家(即:客户端非本地玩家)调用了CmdFire();造成的,
if (!isLocalPlayer)
return;
if (Input.GetButtonDown("Fire1"))
{
CmdFire();
}
[Command]
void CmdFire()
{
RpcBulletControl();
}
2020年09月21日 02点09分 7
level 1
[Command] 这倒霉玩意 和[ClientRpc]这倒霉玩意不能套在一起 从[ClientRpc]调用[Command] 得加个中介
2023年11月09日 02点11分 8
1