我正在为Unity制作Fortnite风格的游戏。
玩家生成,具有生成多维数据集作为“基础”的能力。
一切在单声道中都运行良好,但是我在网络方面遇到了一个奇怪的问题。在服务器上,即使播放器是Player预制件的克隆,我的播放器也可以根据客户端上的Raycast命中点完美地生成多维数据集,但生成的对象总是以世界原点结束,而不是Raycast命中点-或者-如果我从包含Raycast信息的播放器脚本中删除if (!isPlayLocal) {return;}
,则该多维数据集会错误地生成,并且没有相应的材质。
我将尝试查明代码,以便将其放在此处,但我想可能是很多事情。
已在生成的预制件上选中了本地播放器身份验证,并且所有预制件都已在网络管理器中注册。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class BuildingSystemNew : NetworkBehaviour
{
[SerializeField] private Camera playerCamera;
[SerializeField] private GameObject blockTemplatePrefab;
[SerializeField] private GameObject blockPrefab;
[SerializeField] private Material templateMaterial;
[SerializeField] private LayerMask buildableSurfacesLayer;
private bool buildModeOn = false;
private bool canBuild = false;
private bool crossHairOn = false;
private BlockSystem bSys;
public Texture2D crosshairImage;
private int blockSelectCounter = 0;
private GameObject weapon;
private Vector3 buildPos;
private GameObject currentTemplateBlock;
private void Start()
{
bSys = GetComponent<BlockSystem>();
}
private void Update()
{
if (isLocalPlayer == false)
return;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
if (Input.GetKeyDown("e"))
{
buildModeOn = !buildModeOn;
if (buildModeOn)
{
// weapon.SetActive(false);
crossHairOn = true;
}
else
{
// weapon.SetActive(true);
crossHairOn = false;
}
}
if (Input.GetKeyDown("r"))
{
blockSelectCounter++;
if (blockSelectCounter >= bSys.allBlocks.Count) blockSelectCounter = 0;
}
if (buildModeOn)
{
RaycastHit buildPosHit;
if (Physics.Raycast(playerCamera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0)), out buildPosHit, 10, buildableSurfacesLayer))
{
Vector3 point = buildPosHit.point;
buildPos = new Vector3(Mathf.Round(point.x), Mathf.Round(point.y), Mathf.Round(point.z));
canBuild = true;
}
else
{
Destroy(currentTemplateBlock.gameObject);
canBuild = false;
}
}
if (!buildModeOn && currentTemplateBlock != null)
{
Destroy(currentTemplateBlock.gameObject);
canBuild = false;
}
if (canBuild && currentTemplateBlock == null)
{
currentTemplateBlock = Instantiate(blockTemplatePrefab, buildPos, Quaternion.identity);
currentTemplateBlock.GetComponent<MeshRenderer>().material = templateMaterial;
}
if (canBuild && currentTemplateBlock != null)
{
currentTemplateBlock.transform.position = buildPos;
if (Input.GetMouseButtonDown(0))
{
CmdPlaceBlock();
}
else if (Input.GetMouseButtonDown(1))
{
CmdDestroyBlock();
}
}
}
[Command]
public void CmdPlaceBlock()
{
GameObject newBlock = Instantiate(blockPrefab, buildPos, Quaternion.identity);
Block tempBlock = bSys.allBlocks[blockSelectCounter];
newBlock.name = tempBlock.blockName + "-Block";
newBlock.GetComponent<MeshRenderer>().material = tempBlock.blockMaterial;
NetworkServer.SpawnWithClientAuthority(newBlock, connectionToClient);
}
[Command]
private void CmdDestroyBlock()
{
RaycastHit hit;
Ray ray = playerCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
var objectHit = hit.collider.gameObject;
if (hit.collider.gameObject.tag == "Block")
{
Destroy(objectHit);
}
}
}
void OnGUI()
{
if (crossHairOn == true)
{
float xMin = (Screen.width / 2) - (crosshairImage.width / 2);
float yMin = (Screen.height / 2) - (crosshairImage.height / 2);
GUI.DrawTexture(new Rect(xMin, yMin, crosshairImage.width, crosshairImage.height), crosshairImage);
}
}
}
答案 0 :(得分:0)
问题
您正在呼叫
Array
没有参数。
[Command]
public void CmdPlaceBlock()
{
GameObject newBlock = Instantiate(blockPrefab, buildPos, Quaternion.identity);
Block tempBlock = bSys.allBlocks[blockSelectCounter];
newBlock.name = tempBlock.blockName + "-Block";
newBlock.GetComponent<MeshRenderer>().material = tempBlock.blockMaterial;
NetworkServer.SpawnWithClientAuthority(newBlock, connectionToClient);
}
在客户端上被调用,但在服务器
=>带有服务器的局部变量!
例如由于以下原因,Command
在服务器上始终具有默认值buildPos
0,0,0
稍后再行
if(!isLocalPlayer) return;
永远不会在服务器上执行。这同样适用于例如到buildPos = new Vector3(Mathf.Round(point.x), Mathf.Round(point.y), Mathf.Round(point.z));
以及您的blockSelectCounter
可能依赖的其他值。
解决方案
您应将客户端的CmdPlaceBlock
值(以及客户端和服务器上所有其他不同的值)传递到服务器命令,以便服务器知道应将新对象放置在正确的位置:
buildPos
我只添加了一个位置示例,因为它知道客户端和服务器上的位置总是不同的。但这也可能适用于您的其他值,例如//...
if (Input.GetMouseButtonDown(0))
{
CmdPlaceBlock(buildPos);
}
//...
[Command]
public void CmdPlaceBlock(Vector3 spawnPosition)
{
GameObject newBlock = Instantiate(blockPrefab, spawnPosition, Quaternion.identity);
Block tempBlock = bSys.allBlocks[blockSelectCounter];
newBlock.name = tempBlock.blockName + "-Block";
newBlock.GetComponent<MeshRenderer>().material = tempBlock.blockMaterial;
NetworkServer.SpawnWithClientAuthority(newBlock, connectionToClient);
}
。
对所有必须是客户端值而不是服务器值的值进行更改。
请注意,联网方法之间可以传递的类型是有限的!您无法通过任何组件引用。
允许的参数类型为;
- 基本类型(字节,整数,浮点数,字符串,UInt64等)
- 内置Unity数学类型(Vector3,四元数等),
- 基本类型的数组
- 包含允许类型的结构
- NetworkIdentity
- NetworkInstanceId
- NetworkHash128
- 带有NetworkIdentity组件的GameObject。
其他提示
对于可读性和行数,您应该更改例如
blockSelectCounter
简单
if (buildModeOn)
{
// weapon.SetActive(false);
crossHairOn = true;
}
else
{
// weapon.SetActive(true);
crossHairOn = false;
}
并检查不喜欢的
// weapon.SetActive(!buildModeOn);
crossHairOn = buildModeOn;
但是
if (isLocalPlayer == false)
它读/写更简单;)