因此,正如标题所示,我遇到了客户端发送的命令未被触发的问题。
我正在尝试使用的基本功能是,当敌人在玩家面前并且我点击时,该玩家会暂时被击晕。如果我是主持人,效果很好,并且双方都完美注册。
如果我作为客户端玩,我会到达“应该”发送命令的地步,但我注意到我收到一条警告,上面写着“尝试为非本地玩家发送命令”。同样,两端都没有发生任何事情。显然,我必须在客户端做错事,但不知道还有什么其他方法可以解决这个问题。
“问题”代码。
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();
}
}
}
}
}
方法调用
[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;
}
编辑:根据要求,这是两个完整的脚本。
统一播放器配置:
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)