Unity/Photon

04. Photon - ์‹ค์Šต 1 (2) ํ”Œ๋ ˆ์ด์–ด Material ๋ฐ”๊พธ๊ธฐ

๐Ÿ”ท ์‹ค์Šต 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 ์ˆœ์„œ๋กœ ์—ฐ๊ฒฐํ•  ๊ฒƒ

 

 

 

- ๋นŒ๋“œํ•œ ํ›„ ํ…Œ์ŠคํŠธํ•ด๋ณด๋ฉด ์ƒ๋Œ€ ํ”Œ๋ ˆ์ด์–ด ์ƒ‰์ด ๋ฐ”๋€Œ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ

 

 

 

์ขŒ : 1๋ฒˆ์งธ ์ž…์žฅํ•œ ํ”Œ๋ ˆ์ด์–ด, ์šฐ : 2๋ฒˆ์งธ ์ž…์žฅํ•œ ํ”Œ๋ ˆ์ด์–ด

- ๋จผ์ € ์ž…์žฅํ•œ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์ƒ‰์„ ๋ฐ”๊พผ ํ›„ 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

- ๋ฒ„๊ทธ๊ฐ€ ์ˆ˜์ •๋œ ๊ฒƒ ํ™•์ธ