Seb*_*awe 5 c# network-programming unity-game-engine unity-networking
所以正如标题所暗示的那样,我遇到了一个问题,即客户端发送的命令没有被触发。
我正在尝试使用的基本功能是,当一个敌人出现在玩家面前并且我点击时,该玩家将被暂时惊呆。如果我是主持人,效果很好,并且双方都完美注册。
如果我作为客户端玩游戏,我会到达“应该”发送命令的地步,但我注意到我收到一条警告,上面写着“正在尝试为非本地玩家发送命令”。同样,两端都没有任何反应。显然,我一定是在客户端做错了什么,但不知道还有什么其他方法可以解决这个问题。
“问题”代码。
if (Input.GetButtonDown("Fire1")) {
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit = new RaycastHit();
Physics.Raycast(ray, out hit, 20f);
if (hit.transform != null) {
if (hit.rigidbody != null) {
PlayerController controller = hit.rigidbody.GetComponent<PlayerController>();
if (controller != null) {
if (!controller.stunned) {
// Send the stun command to the server
controller.CmdStun();
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
该方法调用
[Command]
public void CmdStun() {
// Report that the stun was sent
UnityEngine.Debug.Log("Stun Sent");
RpcStun();
}
[ClientRpc]
public void RpcStun() {
// Activate the stun timer.
stunTimer.Start();
// Track the players original color.
normalColor = manager.color;
// Make the players bot look like its shut down.
manager.InitiateColorChange(Color.black);
// Other code will check against this before trying to send another stun command.
stunned = true;
}
Run Code Online (Sandbox Code Playgroud)
编辑:根据要求,这里有两个脚本的全部内容。
统一播放器配置:
https://gyazo.com/400c5b3a95c1a58f9b6e930b0c3c853b
https://gyazo.com/c17a7317767a00e2150ff34b03a03e8f
https://gyazo.com/322731aefbe69f9567d2b395523b8f2a
完整的警告信息
尝试为非本地玩家发送命令。UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter, Int32, String) PlayerController:CallCmdStun() ObjectInteractor:Update()(在 Assets/Scripts/ObjectInteractor.cs:58)
确保您的播放器预制件上有一个 NetworkIdentity 组件,并选中了本地播放器权限。然后在问题代码上方,仅在本地播放器时运行它,添加以下内容:
if (!isLocalPlayer)
return;
Run Code Online (Sandbox Code Playgroud)
编辑:如果您正在测试远程,请务必在 Unity 的播放器设置(编辑 -> 项目设置 -> 播放器)的 Cloud Project Id 字段中包含 UPID(通过在https://multiplayer.unity3d.com注册您的项目)客户。即使您使用的是本地主机,远程客户端也会使用 Unity 的中继服务器连接到您的主机游戏。
编辑2:尝试将调用更改为controller.CmdStun()to controller.Stun()。然后PlayerController添加一个Stun()调用私有函数的公共函数CmdStun()。
| 归档时间: |
|
| 查看次数: |
9044 次 |
| 最近记录: |