我开始使用Unity的HLAPI并开始了一个简短的教程,以了解客户端和服务器之间的互动方式:https://www.youtube.com/watch?v=9VW7ctwvNok&t=458s但是当我拨打我的命令时,我收到了错误信息。 ; t出现在教程中。
尝试在没有权限的情况下发送对象命令。 UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter,Int32,String) 的PlayerController:CallCmdSendClientName(字符串) PlayerController:Start()(在Assets / Scripts / PlayerController.cs:87)
尝试在没有权限的情况下发送对象命令。 UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter,Int32,String) 的PlayerController:CallCmdSendClientPos(的Vector3) PlayerController:Start()(在Assets / Scripts / PlayerController.cs:88)
我有一个播放器预制件,它附有一个NetworkIdentity脚本和我的PlayerController脚本,因此我不确定为什么我会收到此错误。任何帮助将不胜感激。谢谢!
public class PlayerController : NetworkBehaviour {
public GameObject bulletPrefab;
public Transform bulletSpawn;
public string playerName;
public Vector3 playerPos;
public byte[] Tex; // data coming from CanvasController
public int texWidth;
public int texHeight;
public string Type; // data coming from CanvasController
public string Id; // data coming from GameManager
public int Strength; // data coming from PlayerController
public int Hitpoints; // data coming from PlayerController
public bool IsAlive; // data coming from PlayerController
private string GetPlayerName()
{
switch (Network.player.ipAddress.ToString())
{
case "192.168.1.160":
playerName = "SMO Server";
break;
case "192.168.1.161":
playerName = "SMO Client 1";
break;
case "192.168.1.162":
playerName = "SMO Client 2";
break;
case "192.168.1.163":
playerName = "SMO Client 3";
break;
case "192.168.1.164":
playerName = "SMO Client 4";
break;
default:
playerName = "SMO UNREG";
break;
}
return playerName;
}
private Vector3 GetPlayerPos()
{
switch (Network.player.ipAddress.ToString())
{
case "192.168.1.160":
playerPos = new Vector3(0, 0, 0);
break;
case "192.168.1.161":
playerPos = new Vector3(-8, 0, 0);
break;
case "192.168.1.162":
playerPos = new Vector3(-3, 0, 0);
break;
case "192.168.1.163":
playerPos = new Vector3(3, 0, 0);
break;
case "192.168.1.164":
playerPos = new Vector3(8, 0, 0);
break;
default:
playerPos = new Vector3(0, 3, 0);
break;
}
return playerPos;
}
private void Start()
{
if (!isServer)
{
CmdSendClientName(GetPlayerName());
CmdSendClientPos(GetPlayerPos());
}
}
[Command]
void CmdSendClientName(string name)
{
RpcUpdateClientName(name);
}
[ClientRpc]
void RpcUpdateClientName(string name)
{
transform.name = name; // Change player name;
}
public override void OnStartLocalPlayer()
{
GetComponent<MeshRenderer>().material.color = Color.blue;
}
[Command]
void CmdSendClientPos(Vector3 pos)
{
RpcUpdateClientPos(pos);
}
[ClientRpc]
void RpcUpdateClientPos(Vector3 pos)
{
transform.position = pos;
}