demonixis

ReplicateXRRig.cs

Oct 21st, 2021
677
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Fusion;
  2. using System;
  3. using UnityEngine;
  4.  
  5. namespace NV.ST
  6. {
  7.     [Serializable]
  8.     public struct XRTransform
  9.     {
  10.         public Vector3 Position;
  11.         public Quaternion Rotation;
  12.     }
  13.  
  14.     [Serializable]
  15.     public struct XRInputData : INetworkInput
  16.     {
  17.         public Vector3 HeadPosition;
  18.         public Quaternion HeadRotation;
  19.  
  20.         public Vector3 LeftHandPosition;
  21.         public Quaternion LeftHandRotation;
  22.  
  23.         public Vector3 RightHandPosition;
  24.         public Quaternion RightHandRotation;
  25.  
  26.         public float HeadOffset;
  27.     }
  28.  
  29.     public sealed class PlayerXRInput : NetworkBehaviour
  30.     {
  31.         XRInputData m_XRInputData;
  32.  
  33.         [SerializeField]
  34.         private bool m_OnlySendInputWhenFocused;
  35.         [SerializeField]
  36.         private Transform m_TrackingSpace = null;
  37.         [SerializeField]
  38.         private Transform m_Head = null;
  39.         [SerializeField]
  40.         private Transform m_LeftHand = null;
  41.         [SerializeField]
  42.         private Transform m_RightHand = null;
  43.  
  44.         public override void Spawned()
  45.         {
  46.             base.Spawned();
  47.  
  48.             if (Object.HasInputAuthority)
  49.             {
  50.                 var networkEvents = FindObjectOfType<NetworkEvents>();
  51.                 networkEvents.OnInput.AddListener(OnInput);
  52.             }
  53.         }
  54.  
  55.         public override void FixedUpdateNetwork()
  56.         {
  57.             base.FixedUpdateNetwork();
  58.  
  59.             if (GetInput(out XRInputData data))
  60.             {
  61.                 if (!Object.HasInputAuthority)
  62.                 {
  63.                     m_Head.localPosition = data.HeadPosition;
  64.                     m_Head.localRotation = data.HeadRotation;
  65.                     m_LeftHand.localPosition = data.LeftHandPosition;
  66.                     m_LeftHand.localRotation = data.LeftHandRotation;
  67.                     m_RightHand.localPosition = data.RightHandPosition;
  68.                     m_RightHand.localRotation = data.RightHandRotation;
  69.  
  70.                     var headOffset = m_TrackingSpace.localPosition;
  71.                     headOffset.y = data.HeadOffset;
  72.                     m_TrackingSpace.localPosition = headOffset;
  73.                 }
  74.             }
  75.         }
  76.  
  77.         private void OnInput(NetworkRunner runner, NetworkInput inputContainer)
  78.         {
  79.             if (m_OnlySendInputWhenFocused && !Application.isFocused)
  80.             {
  81.                 return;
  82.             }
  83.  
  84.             m_XRInputData.HeadPosition = m_Head.localPosition;
  85.             m_XRInputData.HeadRotation = m_Head.localRotation;
  86.             m_XRInputData.LeftHandPosition = m_LeftHand.localPosition;
  87.             m_XRInputData.LeftHandRotation = m_LeftHand.localRotation;
  88.             m_XRInputData.RightHandPosition = m_RightHand.localPosition;
  89.             m_XRInputData.RightHandRotation = m_RightHand.localRotation;
  90.             m_XRInputData.HeadOffset = m_TrackingSpace.localPosition.y;
  91.  
  92.             inputContainer.Set(m_XRInputData);
  93.         }
  94.     }
  95. }
  96.  
RAW Paste Data