๐ท์ค์ต 1 (3)
- Item ๋๋ค ์์ฑ
- Item ๋จน์ผ๋ฉด Score ํ๋
๐ถ Item ๋จน์ผ๋ฉด effect ๋ฐ์
- ์ ๋นํ effect import
- ๋ง์ ๋๋ effect ๊ณจ๋ผ์ 03.Prefabs ํด๋ ์๋๋ก Prefabํ
- ์คํฌ๋ฆฝํธ ์์ฑ : ItemManager
- Item_1, Item_2, Item_3 ๊ฒ์ ์ค๋ธ์ ํธ์ ItemManager ์คํฌ๋ฆฝํธ ์ถ๊ฐ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemManager : MonoBehaviour
{
public GameObject collFx;
void Start()
{
}
private void OnCollisionEnter(Collision other)
{
// ์ดํํธ
GameObject tempFx = Instantiate(collFx, this.transform.position, Quaternion.LookRotation(Vector3.up));
Destroy(tempFx, 2.0f);
// ์๋ฆฌ
}
}
- Item_1, Item_2, Item_3 ๊ฒ์ ์ค๋ธ์ ํธ์ ์ดํํธ ์ฐ๊ฒฐ
- play > Item๊ณผ ์ถฉ๋ > effect ์์ฑ๋๋ ๊ฒ ํ์ธ
๐ถ Item ๋จน์ผ๋ฉด ์๋ฆฌ ๋ฐ์
- ์ ๋นํ SFX import
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemManager : MonoBehaviour
{
public GameObject collFx;
private new AudioSource audio;
public AudioClip collSFX;
void Start()
{
audio = GetComponent<AudioSource>();
}
private void OnCollisionEnter(Collision other)
{
// ์ดํํธ
GameObject tempFx = Instantiate(collFx, this.transform.position, Quaternion.LookRotation(Vector3.up));
Destroy(tempFx, 2.0f);
// ์๋ฆฌ
audio.PlayOneShot(collSFX);
}
}
- Item_1, Item_2, Item_3 ๊ฒ์ ์ค๋ธ์ ํธ์ ์ฌ์ด๋ ์ฐ๊ฒฐ
- ์ปดํฌ๋ํธ ์ถ๊ฐ : Audio Source
๐ถ Item ๋จน์ผ๋ฉด Score ํ๋
- UI ์์ฑ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public static GameManager instance = null;
public PhotonView pv;
public bool isConnect = false;
public Transform[] spawnPoints;
public Text scorePlayer1Text;
public Text scorePlayer2Text;
private int scorePlayer1 = 0;
private int scorePlayer2 = 0;
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(this.gameObject);
}
else if (instance != this)
{
Destroy(this.gameObject);
}
}
void Start()
{
pv = GetComponent<PhotonView>();
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);
}
public void GetScore(int playerNum)
{
pv.RPC(nameof(SetScore), RpcTarget.AllViaServer, playerNum);
}
[PunRPC]
public void SetScore(int playerNum)
{
if (playerNum == 1)
{
scorePlayer1++;
scorePlayer1Text.text = $"{scorePlayer1:00}";
}
else
{
scorePlayer2++;
scorePlayer2Text.text = $"{scorePlayer2:00}";
}
}
}
- GameManager ์คํฌ๋ฆฝํธ
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);
}
}
}
// ๋ฌผ๋ฆฌ์ ์ฒ๋ฆฌ
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)
{
GameManager.instance.GetScore(pv.ViewID / 1000);
GetComponent<Renderer>().material = playerMt[idx];
}
public override void OnPlayerEnteredRoom(Player newPlayer)
{
if (pv.IsMine && idxMt != -1)
{
pv.RPC(nameof(SetMt), newPlayer, idxMt);
}
}
}
- PlayerCtrl ์คํฌ๋ฆฝํธ
- ์์์ ๋ง๊ฒ ์ฐ๊ฒฐ
- play
- ์ฒซ๋ฒ์งธ ์ ์ฅํ player๊ฐ player1
๐ถ Item ๋จน์ผ๋ฉด Item ์๋ฉธ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemManager : MonoBehaviour
{
public GameObject collFx;
private new AudioSource audio;
public AudioClip collSFX;
void Start()
{
audio = GetComponent<AudioSource>();
}
private void OnCollisionEnter(Collision other)
{
// Item Destroy
GetComponent<Collider>().enabled = false;
GetComponent<Renderer>().enabled = false;
Destroy(this.gameObject, 3.0f);
// ์ดํํธ
GameObject tempFx = Instantiate(collFx, this.transform.position, Quaternion.LookRotation(Vector3.up));
Destroy(tempFx, 2.0f);
// ์๋ฆฌ
audio.PlayOneShot(collSFX);
}
}
'Unity > Photon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
07. Photon - ์ค์ต 1 (5) Photon Voice (0) | 2021.08.23 |
---|---|
06. Photon - ์ค์ต 1 (4) Lobby, Custom Property (0) | 2021.08.22 |
04. Photon - ์ค์ต 1 (2) ํ๋ ์ด์ด Material ๋ฐ๊พธ๊ธฐ (0) | 2021.08.17 |
03. Photon - ์ค์ต 1 (1) player ์์ฑ, ์์น ๋๊ธฐํ (0) | 2021.08.16 |
02. Photon ์ฐ๊ฒฐ, ๊ธฐ๋ณธ ๋ฉ์๋ (2) | 2021.08.05 |