๐ท ์ค์ต 1
room์ ์ ์ฅํ๋ฉด์ player ์์ฑ
์์น ๋๊ธฐํ
๐ถ Photon ๊ธฐ๋ณธ ์ธํ
- ํฌํค ์ดํ๋ฆฌ์ผ์ด์ ์์ฑ
- ์์ด๋ ๋ณต์ฌ
- ์ ๋ํฐ PUN2 ํจํค์ง ์ํฌํธ
- ์์ด๋ ๋ถ์ฌ๋ฃ๊ธฐ
๐ท ์ด๋ ๋ก์ง
- ๋น๊ฒ์์ค๋ธ์ ํธ ์์ฑ : PhotonManager
- ์คํฌ๋ฆฝํธ ์์ฑ : PhotonManager
- ๊ฒ์์ค๋ธ์ ํธ์ ์คํฌ๋ฆฝํธ ์ถ๊ฐ
- ํ๋ธ ์์ฑ : Floor
- Scale : 10, 0.1, 10
- ๋น๊ฒ์์ค๋ธ์ ํธ ์์ฑ : GameManager
- ์คํฌ๋ฆฝํธ ์์ฑ : GameManager
- ๋น๊ฒ์์ค๋ธ์ ํธ์ ์คํฌ๋ฆฝํธ ์ถ๊ฐ
- ๋น๊ฒ์ ์ค๋ธ์ ํธ ์์ฑ : SpawnPointGroup
- Position : 0, 0.6, 0
- ํ์์ ๋น๊ฒ์์ค๋ธ์ ํธ ์์ฑ : SpawnPoint_1, SpawnPoint_2
- ๋ง์ฃผ๋ณด๊ณ ์๋๋ก ์ ๋นํ ์์น ์กฐ์
- SpawnPoint_2
- rotate : 0, 180, 0
- Player ๋ง๋ ํ Prefabํ
- ์คํฌ๋ฆฝํธ ์์ฑ : PlayerCtrl
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCtrl : MonoBehaviour
{
private new Rigidbody rigidbody;
private float v;
private float h;
private float r;
[Header("์ด๋ ๋ฐ ํ์ ์๋")]
public float moveSpeed = 8.0f;
public float turnSpeed = 0.0f;
public float jumpPower = 5.0f;
private float turnSpeedValue = 200.0f;
RaycastHit hit;
IEnumerator Start()
{
rigidbody = GetComponent<Rigidbody>();
turnSpeed = 0.0f;
yield return new WaitForSeconds(0.5f);
turnSpeed = turnSpeedValue;
}
// ํค ์
๋ ฅ
void Update()
{
v = Input.GetAxis("Vertical");
h = Input.GetAxis("Horizontal");
r = Input.GetAxis("Mouse X");
Debug.DrawRay(transform.position, -transform.up * 0.6f, Color.green);
if (Input.GetKeyDown("space"))
{
if (Physics.Raycast(transform.position, -transform.up, out hit, 0.6f))
{
rigidbody.AddForce(Vector3.up * jumpPower, ForceMode.Impulse);
}
}
}
// ๋ฌผ๋ฆฌ์ ์ฒ๋ฆฌ
void FixedUpdate()
{
Vector3 dir = (Vector3.forward * v) + (Vector3.right * h);
transform.Translate(dir.normalized * Time.deltaTime * moveSpeed, Space.Self);
transform.Rotate(Vector3.up * Time.smoothDeltaTime * turnSpeed * r);
}
}
- ์คํฌ๋ฆฝํธ ์์ฑ
- rigidbody ์ปดํฌ๋ํธ ์ถ๊ฐ
- PlayerCtrl ์คํฌ๋ฆฝํธ ์ถ๊ฐ
- ํ๋ ์ด์ด ์์ง์ด๋์ง ํ์ธ
- Player ์๋์ ๋น๊ฒ์์ค๋ธ์ ํธ ์์ฑ : CamPivot
- Position : 0, 2, 0
- ์ปดํฌ๋ํธ ์ถ๊ฐ : Photon View, Photon Transform View
- Apply All
- ํ์ด์ด๋ผํค ์ฐฝ์์ player ์ญ์
๐ท ํฌํค ์ฐ๊ฒฐ๋๋ฉด ํ๋ ์ด์ด ์์ฑ
- Resources ํด๋ ์์ฑ
- Player ํ๋ฆฌํน Resources ํด๋๋ก ์ด๋
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
public class GameManager : MonoBehaviour
{
public static GameManager instance = null;
public bool isConnect = false;
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(this.gameObject);
}
else if (instance != this)
{
Destroy(this.gameObject);
}
}
void Start()
{
StartCoroutine(CreatePlayer());
}
void Update()
{
}
IEnumerator CreatePlayer()
{
yield return new WaitUntil(() => isConnect);
GameObject playerTemp = PhotonNetwork.Instantiate("Player", Vector3.one, Quaternion.identity, 0);
}
}
- GameManager ์คํฌ๋ฆฝํธ ์์
- ์ปดํฌ๋ํธ ์ถ๊ฐ : Photon View
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
public class PhotonManager : MonoBehaviourPunCallbacks
{
private readonly string gameVersion = "v1.0";
private string userId = "Ojui";
private void Awake()
{
// ๊ฒ์ ๋ฒ์ ์ง์
PhotonNetwork.GameVersion = gameVersion;
// ์๋ฒ ์ ์
PhotonNetwork.ConnectUsingSettings();
}
void Start()
{
Debug.Log("00. ํฌํค ๋งค๋์ ์์");
PhotonNetwork.NickName = userId;
}
public override void OnConnectedToMaster()
{
Debug.Log("01. ํฌํค ์๋ฒ์ ์ ์");
PhotonNetwork.JoinRandomRoom();
}
public override void OnJoinRandomFailed(short returnCode, string message)
{
Debug.Log("02. ๋๋ค ๋ฃธ ์ ์ ์คํจ");
// ๋ฃธ ์์ฑ ์ค์
RoomOptions ro = new RoomOptions();
ro.IsOpen = true;
ro.IsVisible = true;
ro.MaxPlayers = 30;
// ๋ฃธ์ ์์ฑ > ์๋ ์
์ฅ๋จ
PhotonNetwork.CreateRoom("room_1", ro);
}
public override void OnCreatedRoom()
{
Debug.Log("03. ๋ฐฉ ์์ฑ ์๋ฃ");
}
public override void OnJoinedRoom()
{
Debug.Log("04. ๋ฐฉ ์
์ฅ ์๋ฃ");
GameManager.instance.isConnect = true;
}
}
- GameManager.instance.isConnect = true; ์ถ๊ฐ
- play ํด๋ณด๋ฉด Player ์์ฑ๋จ
- Floor๊ฐ ์ข์์ Scale ์กฐ์ : 50, 0.1, 50
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
public class GameManager : MonoBehaviour
{
public static GameManager instance = null;
public bool isConnect = false;
public Transform[] spawnPoints;
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(this.gameObject);
}
else if (instance != this)
{
Destroy(this.gameObject);
}
}
void Start()
{
StartCoroutine(CreatePlayer());
}
void Update()
{
}
IEnumerator CreatePlayer()
{
yield return new WaitUntil(() => isConnect);
spawnPoints = GameObject.Find("SpawnPointGroup").GetComponentsInChildren<Transform>();
Vector3 pos = spawnPoints[PhotonNetwork.CurrentRoom.PlayerCount].position;
Quaternion rot = spawnPoints[PhotonNetwork.CurrentRoom.PlayerCount].rotation;
GameObject playerTemp = PhotonNetwork.Instantiate("Player", pos, rot, 0);
}
}
- ์์ฑ ์์น ์กฐ์
๐ท ์นด๋ฉ๋ผ Follow
- ์คํฌ๋ฆฝํธ ์์ฑ : SmoothFollow
using UnityEngine;
namespace UnityStandardAssets.Utility
{
public class SmoothFollow : MonoBehaviour
{
// The target we are following
[SerializeField]
public Transform target;
// The distance in the x-z plane to the target
[SerializeField]
private float distance = 10.0f;
// the height we want the camera to be above the target
[SerializeField]
private float height = 5.0f;
[SerializeField]
private float rotationDamping;
[SerializeField]
private float heightDamping;
// Use this for initialization
void Start() { }
// Update is called once per frame
void LateUpdate()
{
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
var wantedRotationAngle = target.eulerAngles.y;
var wantedHeight = target.position.y + height;
var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position = new Vector3(transform.position.x ,currentHeight , transform.position.z);
// Always look at the target
transform.LookAt(target);
}
}
}
- SmoothFollow ์คํฌ๋ฆฝํธ ์ถ๊ฐ
- Distance, Height, Rotation Damping, Height Damping ์ ๋นํ ์กฐ์
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityStandardAssets.Utility;
public class PlayerCtrl : MonoBehaviour
{
private new Rigidbody rigidbody;
private PhotonView pv;
private float v;
private float h;
private float r;
[Header("์ด๋ ๋ฐ ํ์ ์๋")]
public float moveSpeed = 8.0f;
public float turnSpeed = 0.0f;
public float jumpPower = 5.0f;
private float turnSpeedValue = 200.0f;
RaycastHit hit;
IEnumerator Start()
{
rigidbody = GetComponent<Rigidbody>();
pv = GetComponent<PhotonView>();
turnSpeed = 0.0f;
yield return new WaitForSeconds(0.5f);
if (pv.IsMine)
{
Camera.main.GetComponent<SmoothFollow>().target = transform.Find("CamPivot").transform;
}
else
{
GetComponent<Rigidbody>().isKinematic = true;
}
turnSpeed = turnSpeedValue;
}
// ํค ์
๋ ฅ
void Update()
{
v = Input.GetAxis("Vertical");
h = Input.GetAxis("Horizontal");
r = Input.GetAxis("Mouse X");
Debug.DrawRay(transform.position, -transform.up * 0.6f, Color.green);
if (Input.GetKeyDown("space"))
{
if (Physics.Raycast(transform.position, -transform.up, out hit, 0.6f))
{
rigidbody.AddForce(Vector3.up * jumpPower, ForceMode.Impulse);
}
}
}
// ๋ฌผ๋ฆฌ์ ์ฒ๋ฆฌ
void FixedUpdate()
{
if (pv.IsMine)
{
Vector3 dir = (Vector3.forward * v) + (Vector3.right * h);
transform.Translate(dir.normalized * Time.deltaTime * moveSpeed, Space.Self);
transform.Rotate(Vector3.up * Time.smoothDeltaTime * turnSpeed * r);
}
}
}
- PlayerCtrl ์คํฌ๋ฆฝํธ ์์
- playํด์ ์นด๋ฉ๋ผ๊ฐ ์ ๋ฐ๋ผ์ค๋์ง ํ์ธ
๐ท Test
๐ถ Build & Settings
- Fullscreen Mode : Windowed
- Default Screen Width, Height : ์ ๋นํ ์กฐ์
- Ctrl + Shift + B > Build
- Builds ํด๋ ์์ฑ > ํด๋ ์ ํ
๐ถ Test
- ์คํํ์ผ ํด๋ฆญ
- ์ ๋ํฐ ํ๋ก์ ํธ play
- ์กฐ์ ์ ํด๋น ์ฐฝ์ ํ๋ ์ด์ด๋ง ์์ง์ด๊ณ ์ ํํ๋์ง ํ์ธ
- ์นด๋ฉ๋ผ๊ฐ ํด๋น ์ฐฝ์ ํ๋ ์ด์ด๋ฅผ ๋ฐ๋ผ์ค๋์ง ํ์ธ
- ํ๋ ์ด์ด์ ์์ง์์ด ์๋๋ฐฉ ํ๋ ์ด์ด์๊ฒ๋ ๋ณด์ด๋์ง ํ์ธ
'Unity > Photon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
06. Photon - ์ค์ต 1 (4) Lobby, Custom Property (0) | 2021.08.22 |
---|---|
05. Photon - ์ค์ต 1 (3) Score (1) | 2021.08.18 |
04. Photon - ์ค์ต 1 (2) ํ๋ ์ด์ด Material ๋ฐ๊พธ๊ธฐ (0) | 2021.08.17 |
02. Photon ์ฐ๊ฒฐ, ๊ธฐ๋ณธ ๋ฉ์๋ (2) | 2021.08.05 |
01. Photon Setting (0) | 2021.07.28 |