NetworkTransform.clientMoveCallback3D

切换到手册

描述

可用于在服务器上验证客户端授权对象移动情况的回调。

此回调版本适用于使用 3D 物理设置的对象。此回调函数可能会返回 false 以完全拒绝移动请求。它还可能修改按引用传递的移动参数。

以下示例在 OnStartServer 中设置回调,并断开在多次失败后将对象移动到无效位置的客户端的连接。

using UnityEngine;
using UnityEngine.Networking;

public class MyMover : NetworkManager { public int cheatCount = 0;

public bool ValidateMove(ref Vector3 position, ref Vector3 velocity, ref Quaternion rotation) { Debug.Log("pos:" + position); if (position.y > 9) { position.y = 9; cheatCount += 1; if (cheatCount == 10) { Invoke("DisconnectCheater", 0.1f); } }

return true; }

void DisconnectCheater() { GetComponent<NetworkIdentity>().connectionToClient.Disconnect(); }

public override void OnStartServer() { GetComponent<NetworkTransform>().clientMoveCallback3D = ValidateMove; } }

这种服务器端移动验证应与客户端移动验证配合使用。如果客户端通过作弊绕过客户端移动检查,则回调应只检测故障。