我正在编写一个多人游戏,但我坚持使用构建系统 - 我的意思是我已经创建了一个代码来放置块它在主机端工作得非常好(当你在主机上运行这个脚本时,每个客户端都会生成块)但是它不能在客户端工作(块只为本地客户端生成,而不是为服务器和其他客户端生成)。我见过很多类似的问题,但今天没有找到答案。也许你们可以帮我一把。这是我的代码:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class Builder : NetworkBehaviour {
public GameObject preview;
public Transform currentPreview;
bool isPreviewing = false;
GameObject buildingPreview;
private NetworkIdentity networkId;
// Use this for initialization
void Start ()
{
networkId = GetComponent<NetworkIdentity>();
}
// Update is called once per frame
void ViewPreview()
{
buildingPreview = Instantiate(preview, transform.position, transform.rotation) as GameObject;
currentPreview = buildingPreview.transform;
isPreviewing = true;
}
void Update ()
{
CmdBuild();
}
void CmdBuild()
{
if (networkId.isLocalPlayer)
{
}
else
{ return; }
if (Input.GetKeyDown(KeyCode.E))
{
if (!isPreviewing)
ViewPreview();
else
{
Destroy(buildingPreview);
isPreviewing = false;
}
}
if (isPreviewing)
{
Preview();
}
}
[Command]
void CmdSpawnBuilding()
{
GameObject buildingPlaced = Instantiate(preview, currentPreview.position, currentPreview.rotation) as GameObject;
NetworkServer.Spawn(buildingPlaced);
}
void Preview()
{
currentPreview.position = transform.position + transform.forward * 3f;
currentPreview.rotation = transform.rotation;
if (Input.GetButtonDown("Fire1"))
{
CmdSpawnBuilding();
isPreviewing = false;
}
}
}
PS:我已将网络身份脚本分配给预制件我正在创建网络感知,并且我在我的网络管理器中将该预制件作为已注册的可生成预制件。