본문 바로가기

카테고리 없음

Unity 포톤클라우드를 이용한 단순한 멀티공간 스크립트

728x90

GameManager.cs

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

public class GameManager : MonoBehaviour
{
    public static GameManager Instance { get; private set; }

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
        {
            Destroy(gameObject);
        }
    }

    public void StartGame(GameObject playerPrefab)
    {
        Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
    }
}

PhotonManager.cs

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

public class PhotonManager : MonoBehaviourPunCallbacks
{
    // Start is called before the first frame update
    void Start()
    {
        PhotonNetwork.ConnectUsingSettings();
    }

    public override void OnConnectedToMaster()
    {
        Debug.Log("Connected to master server");
        PhotonNetwork.JoinOrCreateRoom("Room", new Photon.Realtime.RoomOptions { MaxPlayers = 4 }, null);
    }

    public override void OnJoinedRoom()
    {
        Debug.Log("Joined room");
        GameObject Player = PhotonNetwork.Instantiate("PlayerPrefab", Vector3.zero, Quaternion.identity);
        GameManager.Instance.StartGame(Player);
    }
}

 

CameraController.cs

using UnityEngine;

public class CameraController : MonoBehaviour
{
    public float smoothTime = 0.3f;
    public Transform target;
    public Vector3 offset;

    private Vector3 velocity = Vector3.zero;

    private void LateUpdate()
    {
        if (target == null)
        {
            return;
        }

        Vector3 targetPosition = target.position + offset;
        transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
        transform.LookAt(target);
    }
}

 

PlayerController2.cs

using UnityEngine;
using Photon.Pun;
using Photon.Realtime;

public class PlayerController2 : MonoBehaviourPun, IPunObservable
{
    public float speed = 10.0f; // 플레이어 이동 속도

    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();

        if (!photonView.IsMine)
        {
            // 다른 플레이어의 경우, Rigidbody를 사용하지 않고 비활성화합니다.
            rb.isKinematic = true;
        }
    }

    void FixedUpdate()
    {
        if (photonView.IsMine)
        {
            // 키보드 입력 처리
            float moveHorizontal = Input.GetAxis("Horizontal");
            float moveVertical = Input.GetAxis("Vertical");

            // 이동 처리
            Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
            rb.AddForce(movement * speed);
        }
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            // 플레이어의 위치와 회전 정보를 전송합니다.
            stream.SendNext(transform.position);
            stream.SendNext(transform.rotation);
        }
        else
        {
            // 다른 플레이어의 위치와 회전 정보를 수신합니다.
            transform.position = (Vector3)stream.ReceiveNext();
            transform.rotation = (Quaternion)stream.ReceiveNext();
        }
    }
}

728x90