我的 Unity 版本是 5.2.3f1,我正在尝试同步子游戏对象的旋转,在本地工作得非常好,但它没有出现在其他客户端中。我尝试了所有我能找到的东西,但一无所获。
这样做的原因是要旋转 FPS 身体,所以,我正在尝试旋转 Spine2(旋转相机不是我最好的解决方案)。我正在使用 Mixamo 角色进行测试,最后我将拥有 Mixamo 自动装配的角色,因此我在这里制作的所有内容都将兼容。
我尝试使用 Network Transform Rigidbody 3D,它只同步角色本身,而不是 Spine2,我尝试过 Network Transform Child 和官方骨架同步。
在脚本部分,我尝试了很多东西,最有希望的是这样的:
[SyncVar]
private Quaternion syncPlayerRotation;
[SerializeField]
private Transform playerTransform;
[SerializeField]
private float lerpRate = 15f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void LateUpdate () {
TransmitRotations();
LerpRotations();
}
void LerpRotations()
{
if (!isLocalPlayer)
playerTransform.localRotation = Quaternion.Lerp(playerTransform.localRotation, syncPlayerRotation, Time.deltaTime * lerpRate);
}
[Command]
void CmdProvideRotationsToServer(Quaternion playerRot)
{
syncPlayerRotation = playerRot;
}
[Client]
void TransmitRotations()
{
if (isLocalPlayer)
{
CmdProvideRotationsToServer(playerTransform.localRotation);
}
}
它来自 youtube 上的 UNET 教程系列,Gamer To Game Developer 用户。
我将它附加到 Spine2 上,但仍然无法正常工作,但是当我将其附加到主角时,它起作用了。
也试过这个:
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
Vector3 syncPosition = Vector3.zero;
if (stream.isWriting)
{
syncPosition = Spine.GetComponent<Rigidbody>().position;
stream.Serialize(ref syncPosition);
}
else
{
stream.Serialize(ref syncPosition);
Spine.GetComponent<Rigidbody>().position = syncPosition;
}
}
但我认为这是针对旧版本的 Unity。
为了进行旋转,我正在使用A Free Simple Smooth Mouselook
我编辑了它,这行:
if (Input.GetMouseButton(1))
{
var xRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.forward);
transform.localRotation = xRotation;
}
else
{
var xRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.right);
transform.localRotation = xRotation;
}
基本上,我将 Vector3.right 更改为 Vector3.forward 并仅在未按下鼠标右键的情况下转换 Vector3.right。该脚本附加到 Spine2 并在开始时通过脚本激活 if(isLocalPlayer)。
有一张当前层次结构的图片:
(有些摄像头只是为了测试,主摄像头是FirstPersonCamera,提取自标准资产)
我注意到,如果我调试 Spine2 旋转日志,它只会给我从 0 到 1 的值。