Unity/Photon

05. Photon - ์‹ค์Šต 1 (3) Score

๐Ÿ”ท์‹ค์Šต 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);
    }
}