๐ท ์ค์ต 1 (2)
- ํ๋ ์ด์ด Material ๋ฐ๊พธ๊ธฐ
- MonoBehaviourPunCallbacks
- PunRPC / GetComponent<PhotonView>().RPC(nameof(ํจ์๋ช ), RpcTarget.AllViaServer, ํ๋ผ๋ฏธํฐ);
- public override void OnPlayerEnteredRoom(Player newPlayer)
- Player๊ฐ ๋ฌผ์ฒด์ ๋ถ๋ชํ์ ๋ ๋์ด์ง์ง ์๋๋ก Freeze Rotation ์ฒดํฌ
- Sphere ์์ฑ : Item_1
- Position : 0, 0.6, 0
- Material ์์ฑํด์ ์ ์ฉ
- ์์ ๊ฐ์ด Item_2, Item_3 ์์ฑ
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 = 200.0f;
public float jumpPower = 5.0f;
private float turnSpeedValue = 0.0f;
private RaycastHit hit;
public Material[] playerMt;
IEnumerator Start()
{
rigidbody = GetComponent<Rigidbody>();
pv = GetComponent<PhotonView>();
turnSpeedValue = turnSpeed;
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);
}
}
private void OnCollisionEnter(Collision other)
{
string coll = other.gameObject.name;
switch (coll)
{
case "Item_1":
pv.RPC(nameof(SetMt), RpcTarget.AllViaServer, 0);
break;
case "Item_2":
pv.RPC(nameof(SetMt), RpcTarget.AllViaServer, 1);
break;
case "Item_3":
pv.RPC(nameof(SetMt), RpcTarget.AllViaServer, 2);
break;
}
}
[PunRPC]
private void SetMt(int idx)
{
GetComponent<Renderer>().material = playerMt[idx];
}
}
- ์คํฌ๋ฆฝํธ ์์
- ๋จธํฐ๋ฆฌ์ผ ์ฐ๊ฒฐ
- ์์ ์ฃผ์ : 1, 2, 3 ์์๋ก ์ฐ๊ฒฐํ ๊ฒ
- ๋น๋ํ ํ ํ ์คํธํด๋ณด๋ฉด ์๋ ํ๋ ์ด์ด ์์ด ๋ฐ๋๋ ๊ฒ์ ํ์ธํ ์ ์์
- ๋จผ์ ์ ์ฅํ ํ๋ ์ด์ด๊ฐ ์์ ๋ฐ๊พผ ํ 2๋ฒ์งธ ํ๋ ์ด์ด๊ฐ ์ ์ฅํ๋ฉด, ์ฒซ๋ฒ์งธ ํ๋ ์ด์ด์ ์์ด ์ ์ฉ์ด ์๋์ด ์์
๐ถ ๋ฒ๊ทธ ์์
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityStandardAssets.Utility;
public class PlayerCtrl : MonoBehaviourPunCallbacks
{
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 = 200.0f;
public float jumpPower = 5.0f;
private float turnSpeedValue = 0.0f;
private RaycastHit hit;
public Material[] playerMt;
private int idxMt = -1;
IEnumerator Start()
{
rigidbody = GetComponent<Rigidbody>();
pv = GetComponent<PhotonView>();
turnSpeedValue = turnSpeed;
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);
}
}
Debug.Log($"update.{idxMt}");
}
// ๋ฌผ๋ฆฌ์ ์ฒ๋ฆฌ
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);
}
}
private void OnCollisionEnter(Collision other)
{
string coll = other.gameObject.name;
switch (coll)
{
case "Item_1":
idxMt = 0;
pv.RPC(nameof(SetMt), RpcTarget.AllViaServer, idxMt);
break;
case "Item_2":
idxMt = 1;
pv.RPC(nameof(SetMt), RpcTarget.AllViaServer, idxMt);
break;
case "Item_3":
idxMt = 2;
pv.RPC(nameof(SetMt), RpcTarget.AllViaServer, idxMt);
break;
}
}
[PunRPC]
private void SetMt(int idx)
{
GetComponent<Renderer>().material = playerMt[idx];
}
public override void OnPlayerEnteredRoom(Player newPlayer)
{
Debug.Log($"1.{idxMt}");
if (pv.IsMine && idxMt != -1)
{
Debug.Log($"2.{idxMt}");
pv.RPC(nameof(SetMt), newPlayer, idxMt);
}
}
}
- public class PlayerCtrl : MonoBehaviourPunCallbacks
- public override void OnPlayerEnteredRoom(Player newPlayer)
- ์๋ก ์ ์ฅํ ํ๋ ์ด์ด์๊ฒ ๋ณธ์ธ์ ์์์ ์๋ ค์ค
- ๋น๋ > play
- ๋ฒ๊ทธ๊ฐ ์์ ๋ ๊ฒ ํ์ธ
'Unity > Photon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
06. Photon - ์ค์ต 1 (4) Lobby, Custom Property (0) | 2021.08.22 |
---|---|
05. Photon - ์ค์ต 1 (3) Score (1) | 2021.08.18 |
03. Photon - ์ค์ต 1 (1) player ์์ฑ, ์์น ๋๊ธฐํ (0) | 2021.08.16 |
02. Photon ์ฐ๊ฒฐ, ๊ธฐ๋ณธ ๋ฉ์๋ (2) | 2021.08.05 |
01. Photon Setting (0) | 2021.07.28 |