관리 메뉴

caLAB

[유니티 VR 네트워크 개발] PC와 VR 플레이어 동기화 본문

Unity/유니티VR

[유니티 VR 네트워크 개발] PC와 VR 플레이어 동기화

도이(doi) 2021. 10. 20. 18:33
728x90

PC와 VR 장비로 접속하여 position과 rotation을 동기화시키는 것을 구현하였다.

다른 게임에서 생긴 캐릭터 동기화시키는 스크립트

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
using Photon.Pun;
using UnityEngine.XR.Interaction.Toolkit;

public class networkPlayer : MonoBehaviour
{
    public Transform head;
    public Transform leftHand;
    public Transform rightHand;
    private PhotonView photonView;

    private Transform headRig;
    private Transform leftHandRig;
    private Transform rightHandRig;

    private void Start()
    {
        photonView = GetComponent<PhotonView>();
        XRRig rig = FindObjectOfType<XRRig>();

        if (networkMgr.Instance.currentPlayer == networkMgr.PlayerState.pcPlayer && photonView.IsMine)
        {
            headRig = GameObject.Find("pcPlayer/Camera").transform;
            leftHandRig = GameObject.Find("pcPlayer/leftHand").transform;
            rightHandRig = GameObject.Find("pcPlayer/rightHand").transform;
        }
        if(networkMgr.Instance.currentPlayer == networkMgr.PlayerState.vrPlayer && photonView.IsMine)
        {
            headRig = rig.transform.Find("Camera Offset/Main Camera");
            leftHandRig = rig.transform.Find("Camera Offset/LeftHand Controller");
            rightHandRig = rig.transform.Find("Camera Offset/RightHand Controller");
        }
    }

    private void Update()
    {
        if(photonView.IsMine)
        {
            rightHand.gameObject.SetActive(false);
            leftHand.gameObject.SetActive(false);
            head.gameObject.SetActive(false);

            MapPosition(head, headRig);
            MapPosition(leftHand, leftHandRig);
            MapPosition(rightHand, rightHandRig);
        }
    }

    void MapPosition(Transform target, Transform rigTransform)
    {
        target.position = rigTransform.position;
        target.rotation = rigTransform.rotation;
    }
}

네트워크 매니저 스크립트

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun; 
using Photon.Realtime; 

public class networkMgr : MonoBehaviourPunCallbacks 
{
    public enum PlayerState
    {
        pcPlayer, vrPlayer
    }
    public PlayerState currentPlayer;

    public GameObject vrPlayer;
    public GameObject pcPlayer;

    public GameObject mainCam;
    public GameObject selectUI;
    private GameObject spawnedPlayerPrefab;

    private static networkMgr _instace;
    public static networkMgr Instance { get { return _instace; } }

    public delegate void playerSelectDele();
    public event playerSelectDele playerselect;

    private void Awake()
    {
        if(_instace == null)
        {
            _instace = this;
        }
    }

    private void Start()
    {
        PhotonNetwork.AutomaticallySyncScene = false;
        startConnectServer();
    }

    public void startConnectServer()
    {
        PhotonNetwork.ConnectUsingSettings();
        Debug.Log("Try to connecting...");

    }

    public void ConnectToServer()
    {
        PhotonNetwork.ConnectUsingSettings();
        Debug.Log("Try connect to server....");
    }

    public override void OnConnectedToMaster()
    {
        Debug.Log("Connected To server. Success");
        base.OnConnectedToMaster();
        PhotonNetwork.JoinLobby();

        PhotonNetwork.JoinOrCreateRoom("room", new RoomOptions()
        {
            MaxPlayers = 10,
            IsOpen = true,
            IsVisible = true
        },
        TypedLobby.Default);
    }

    public override void OnJoinedLobby()
    {
        base.OnJoinedLobby();
        Debug.Log("joined lobby");
    }

    public override void OnJoinedRoom()
    {
        Debug.Log("Joined a Room");
        base.OnJoinedRoom();
        selectUI.SetActive(true);
    }

    public void pcPlayerSelect()
    {
        mainCam.SetActive(false);
        selectUI.SetActive(false);
        pcPlayer.SetActive(true);
        currentPlayer = PlayerState.pcPlayer;
        spawnedPlayerPrefab = PhotonNetwork.Instantiate("spawnedPCplayer", transform.position, transform.rotation);
    }

    public void vrPlayerSelect()
    {
        mainCam.SetActive(false);
        selectUI.SetActive(false);
        vrPlayer.SetActive(true);
        currentPlayer = PlayerState.vrPlayer;
        spawnedPlayerPrefab = PhotonNetwork.Instantiate("spawnedVRplayer", transform.position, transform.rotation);
    }

    public override void OnLeftRoom()
    {
        base.OnLeftRoom();
        PhotonNetwork.Destroy(spawnedPlayerPrefab);
    }

    public override void OnPlayerEnteredRoom(Player newPlayer)
    {
        Debug.Log("a new player joined the room.");
        base.OnPlayerEnteredRoom(newPlayer);
    }

    public override void OnPlayerLeftRoom(Player otherPlayer)
    {
        Debug.Log("a player left the room");
        base.OnPlayerLeftRoom(otherPlayer);
    }

    public void Disconnect()
    {
        base.OnLeftRoom();
        PhotonNetwork.Disconnect();
        PhotonNetwork.LoadLevel("Lobby");
    }

    public override void OnDisconnected(DisconnectCause cause)
    {
        base.OnDisconnected(cause);
        Debug.Log("Disconnected");
    }
}
728x90
반응형
Comments