0
\$\begingroup\$

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?

\$\endgroup\$
2
  • \$\begingroup\$ Why are you doing an Cmd/ Rpc call if you are using the Network Transform? The component should take care of everything regarding syncing it already, you use it locally just as you would use any normal local manipulation. The component is handling the rest. Take a look at the tank example in Mirror example folder, movement is handled by the component, shooting is th]e usual command/ rpc. \$\endgroup\$ Commented Apr 24, 2023 at 13:33
  • \$\begingroup\$ Network transform doesn't update child objects. In this case the child object is the avatar vr hooks that are needed for the kinetic animation. VR avatar is rigged based on this tutorial youtube.com/watch?v=tBYl-aSxUe0 \$\endgroup\$ Commented Apr 24, 2023 at 16:52

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.