I'm first time trying to setup multiplayer/co-op game mode for the VR game. Currently just trying to get the avatars to move similar fashion to all clients.
Issue atm is that I can read the head orientation correctly from all the clients but the command + rpclient combo is not working (most likely I've just misunderstood something).
Code currently is like so:
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class NetworkPlayer : NetworkBehaviour
{
#region serializables
[SerializeField] private Transform rigTransform;
[SerializeField] private Transform headTransform;
[SerializeField] private Transform rightHandTransform;
[SerializeField] private Transform leftHandTransform;
#endregion
public VRRig vrRig;
public GameObject XRPlayerPrefab;
private GameObject xrPlayer;
// Start is called before the first frame update
public override void OnStartLocalPlayer()
{
if (isLocalPlayer && isOwned)
{
vrRig.localRig = false;
rigTransform = vrRig.transform;
rigTransform.SetParent(transform);
//Setting up XR rig for client
xrPlayer = Instantiate(XRPlayerPrefab);
xrPlayer.transform.SetParent(transform);
ClientXRRig cxrRig = xrPlayer.GetComponent<ClientXRRig>();
vrRig.head.vrTarget = cxrRig.Head.transform;
vrRig.leftHand.vrTarget = cxrRig.LeftController.transform;
vrRig.rightHand.vrTarget = cxrRig.RightController.transform;
rightHandTransform = vrRig.rightHand.vrTarget;
leftHandTransform = vrRig.leftHand.vrTarget;
// This transform should contain information over the network
headTransform = vrRig.head.rigTarget;
}
}
// Update is called once per frame
void Update()
{
if (!isLocalPlayer)
{
return;
}
UpdateAvatar();
if(headTransform != null)
{
CmdUpdateHeadTurn(headTransform.rotation);
}
}
void UpdateAvatar()
{
rigTransform.position = vrRig.headConstraint.position + vrRig.headBodyOffset;
rigTransform.forward = Vector3.ProjectOnPlane(vrRig.headConstraint.up, Vector3.up).normalized;
vrRig.head.Map();
vrRig.leftHand.Map();
vrRig.rightHand.Map();
headTransform = vrRig.head.rigTarget;
}
[Command]
private void CmdUpdateHeadTurn(Quaternion rotation)
{
RpcOnHeadTurn(rotation);
Debug.Log(gameObject.name + " send head rotation to server "+rotation.ToString());
}
[ClientRpc]
private void RpcOnHeadTurn(Quaternion newRotation)
{
headTransform.rotation = newRotation;
if (!isLocalPlayer)
{
vrRig.head.Map(headTransform);
Debug.Log(gameObject.name + " updated headTranform from server "+newRotation.ToString());
}
}
}
Right now everything works on local client. The avatar is smooth, but other clients are seen as t-pose that is moving and rotating thanks to Network Transform (reliable) component. Network Transform + Network Identity components are in same prefab where the NetworkPlayer is also.
Right now hoping to make the head turn based on client input and then move to arms once the head is working.
Any idea that why the head rotation is not updated correctly?