Unity/์œ ๋‹ˆํ‹ฐ ํฌํ†ค(๋„คํŠธ์›Œํฌ)

ํฌํ†ค ๋ฌด๋ฃŒ๋ฒ„์ „ ์‚ฌ์šฉํ•˜๊ธฐ(2) ํ”Œ๋ ˆ์ด์–ด ๋‹ค์ˆ˜ ์ƒ์„ฑ๊ณผ ์ƒํ˜ธ ์ƒํƒœ ๋™๊ธฐํ™”๋ฅผ ์œ„ํ•œ ๊ธฐ์ดˆ์ž‘์—…

Rainbow๐ŸŒˆCoder 2022. 1. 4. 12:03
728x90

์•ž์„  ๊ธ€๊ณผ ์ด์–ด์„œ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

์ ๋‹นํžˆ ์บก์А ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ  ์ด๋ฆ„์€ 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();
        }
    }

}

 

728x90