์์ ๊ธ๊ณผ ์ด์ด์ ์์ฑํ๋ ๊ฒ์ ๋๋ค.
์ ๋นํ ์บก์ ํ๋ ๋ง๋ค๊ณ ์ด๋ฆ์ player.
rigidbody์ปดํฌ๋ํธ์ PlayerController ํ๋ ๋ฌ์์ฃผ๊ณ (์ถํ ์์ฑ),
Photon View ์ปดํฌ๋ํธ์ Photon Transform View ์ปดํฌ๋ํธ๋ ๋ฌ์์ค๋ค.
player๊ฐ ์์ง์ผ ์ง๋ฉด์ด ๋ ํ๋ธ ํ๋ ์์ฑํ์ฌ x์ z์ Scale ๊ฐ์ ์์ ๋กญ๊ฒ ์กฐ์ .
์ด ์บก์์ ํ๋ ์ด์ด๋ก ํ๋ํ ๊ฒ์ด๋ค.
์ด ์บก์์ Resources ํด๋๋ฅผ ์๋ก๋ง๋ค์ด์
๊ทธ ์์ ํ๋ฆฌํฉ์ผ๋ก ๋ง๋ค๊ณ ํ์ด์ด๋ผํค ์ฐฝ ์์ ์๋ player๋ ์ง์ด๋ค.
๋คํธ์ํฌ๋งค๋์ .cs๋ฅผ ์๋์ ๊ฐ์ด ์์ ํด์ค๋ค.
public GameObject playerprefab;
public Transform spawnspot;
void SpawnPlayer()
{
//ํฌํค ๋คํธ์ํฌ ํ๋ฆฌํฉ ์์ฑํจ์
PhotonNetwork.Instantiate("player", spawnspot.position, Quaternion.identity);
}
ํด๋น ๊ตฌ๋ฌธ์ด ์ถ๊ฐ๊ฐ ๋๋ฉด
์ธ์คํํฐ ์ฐฝ์ Resources ์์ ์๋ player ํ๋ฆฌํฉ์ ๋์ด ๋ฃ๋๋ค.
<ํ ์์ค>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
//์์ ๋ฐ์ ํฌํคํด๋์ค์ ํจ์๋ค์ ์ฌ์ ์ํด์ ์ด๋ค.
public class NetworkManager : MonoBehaviourPunCallbacks
{
public GameObject playerprefab;
public Transform spawnspot;
private void Awake()
{
PhotonNetwork.AutomaticallySyncScene = true;
Debug.Log("TEST");
//ํฌํค ๋คํธ์ํฌ์ ์ ์
PhotonNetwork.GameVersion = "1.0";
PhotonNetwork.NickName = "test";
PhotonNetwork.ConnectUsingSettings();
}
public override void OnConnectedToMaster()
{
Debug.Log("OnConnectedToMaster() ๋ง์คํฐ์ ์ฐ๊ฒฐ๋์๋ค");
PhotonNetwork.JoinRandomRoom();
}
// ๋๋ค ๋ฃธ ์
์ฅ์ ์คํจํ์ ๋ ํธ์ถ ๋๋ ํจ์
public override void OnJoinRandomFailed(short returnCode, string message)
{
Debug.Log("๋ฐฉ์ด ์์");
PhotonNetwork.CreateRoom("TEST");
}
public override void OnCreatedRoom()
{
Debug.Log("OnCreatedRoom() ๋ฃธ์ ๋ง๋๋ ๋ฐ ์ฑ๊ณตํ๋ค");
}
public override void OnJoinedRoom()
{
Debug.Log("OnJoinedRoom() ๋ฃธ์ ๋ค์ด์๋ค");
SpawnPlayer();
}
void SpawnPlayer()
{
//ํฌํค ๋คํธ์ํฌ ํ๋ฆฌํฉ ์์ฑํจ์
PhotonNetwork.Instantiate("player", spawnspot.position, Quaternion.identity);
}
}
PlayerController ์ ์์ฑ.
using Photon.Pun;
using Photon.Realtime; ์ถ๊ฐ
MonoBehaviourPunCallbacks, IPunObservable ์์
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
//๊ด์ธก ์ปจํธ๋กค๋ฌ
public class playerController : MonoBehaviourPunCallbacks, IPunObservable
{
private Vector3 curPos;
private Quaternion curRot;
void Start()
{
}
void Update()
{
if(photonView.IsMine)//๋ด๊ฐ ํด๋ผ์ด์ธํธ์ผ๋
{
//ํ์์ ๊ตฌํํ๋ ํ๋ ์ด์ด ๋ก์ง์ ์ฌ๊ธฐ์๋ค๊ฐ ๊ตฌํํ๋ฉด ๋๋ค.
float v = Input.GetAxis("Vertical");
float h = Input.GetAxis("Horizontal");
Vector3 moveDir = (Vector3.forward * v) + (Vector3.right * h);
transform.Translate(moveDir*Time.deltaTime*10f);
}
else //๋๊ธฐํ ์์ผ์ฃผ๋ ๋ถ๋ถ
{
transform.position = curPos;
transform.rotation = curRot;
}
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if(stream.IsWriting)//์ฐ๋ ์ํ, ์๋๋ฐฉ์๊ฒ ๋์ ์ ๋ณด๋ฅผ ๋ณด๋(์ ๋๋ฉ์ด์
, ์๊น ๋ณํ.. ๋ฑ๋ฑ)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else//๋ณด๋ธ๊ฑธ ๋๊ฐ์ด ์์๋๋ก ๋ฐ์
{
curPos = (Vector3)stream.ReceiveNext();
curRot = (Quaternion)stream.ReceiveNext();
}
}
}