Notice
                              
                          
                        
                          
                          
                            Recent Posts
                            
                        
                          
                          
                            Recent Comments
                            
                        
                          
                          
                            Link
                            
                        
                    | 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 
| 9 | 10 | 11 | 12 | 13 | 14 | 15 | 
| 16 | 17 | 18 | 19 | 20 | 21 | 22 | 
| 23 | 24 | 25 | 26 | 27 | 28 | 29 | 
| 30 | 
                            Tags
                            
                        
                          
                          - W3Schools
 - 프로그래밍
 - Unreal Engine 5
 - w3school
 - Material
 - github
 - 기초
 - dynamic
 - dfs
 - 백준
 - C#
 - Algorithm
 - UE5
 - Unity
 - 재귀
 - Tutorial
 - c++
 - 파이썬
 - 문제풀이
 - Class
 - parameter
 - String
 - 시작해요 언리얼 2022
 - python
 - loop
 - 오류
 - Basic
 - guide
 - Programming
 - DP
 
                            Archives
                            
                        
                          
                          - Today
 
- Total
 
행복한 개구리
Unity 팀프 21.05.31. PUN2 - CustomProperties(R&D) 본문
      Unity/Project : Cursed Treasure
      
    Unity 팀프 21.05.31. PUN2 - CustomProperties(R&D)
HappyFrog 2021. 6. 1. 00:13아무리봐도 기능사용에는 문제가 없다. 현재 짐작가는 부분은
- PlayerControllerScirpt에서 Trigger처리를 하기 때문에 1~4번중 한 객체가 OnTriggerEnter된다면 나머지 객체에서도 OnTriggerEnter메서드가 실행된다는 가정.
 - Player들이 각자의 CustomProperty를 가지고 있지 않아서 겹친다는 가정
 - 아니면 OnPlayerPropertiesUpdate가 모든 플레이어를 불러온다는 가정
 
여기서 3번째 가정은 틀렸다. 이유는 만약 1~3번중 2번이 유물을 먹어 CustomProperty의 값이 바뀌었다면, 2번에 대한 정보가 전부 로그로 출력이 된 뒤에 3번,1번으로 바뀐값을 출력했다. 따라서 OnPropertiesUpdate는 바뀐 플레이어만 갱신해 주는 기능이 맞다.
1번째 가정도 틀렸다. Unity와 Build로 둘 다 실행하고 한 방에서 Unity의 PlayerController와 CapsuleCollider를 꺼보았지만, 출력은 여전히 유물을 먹은 2번의 프로퍼티가 갱신되고, 1번의 OnTriggerEnter가 실행되며 프로퍼티가 한번 더 갱신된다.
ㄴ혹시나해서 아예 컴포넌트를 제거해보았지만 결과는 같다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;
using Hashtable = ExitGames.Client.Photon.Hashtable;
public class InGame : MonoBehaviourPunCallbacks, IPunObservable
{
    public List<Transform> spawnPoints;
    public CameraController camController;
    public Button btn;
    //[HideInInspector]
    //public Player playerCharacter;
    bool isLocal;
    bool s1 = true;
    GameObject character;
    Vector3 pos;
    [HideInInspector]
    public int idx;
    Hashtable hash;
    Dictionary<string, object> playerInfo;
    
    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        stream.SendNext(this.character.transform.position);
        pos = (Vector3)stream.ReceiveNext();
        
    }
    private void Awake()
    {
        PhotonNetwork.SendRate = 60;
        PhotonNetwork.SerializationRate = 60;
        
    }
    void Start()
    {
        idx = 0;
        //hash = new Hashtable();
        Init();
        
    }
    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            this.character.transform.Translate(0, 0, 0.1f);
        }
        if (Input.GetKey(KeyCode.S))
        {
            this.character.transform.Translate(0, 0, -0.1f);
        }
        if (Input.GetKey(KeyCode.A))
        {
            this.character.transform.Translate(-0.1f, 0, 0);
        }
        if (Input.GetKey(KeyCode.D))
        {
            this.character.transform.Translate(0.1f, 0, 0);
        }
    }
    void Init()
    {
        SpawnCharacter();
        SpawnTreasure();
    }
    void SpawnTreasure()
    {
        if (PhotonNetwork.LocalPlayer.IsMasterClient)
            PhotonNetwork.Instantiate("Treasure", Vector3.zero, Quaternion.identity);
    }
    void SpawnCharacter()
    {
        foreach (var player in PhotonNetwork.PlayerList)
        {            
            if (player.IsLocal)
            {
                var spawnPosition = spawnPoints[idx].position;
               
                this.character = PhotonNetwork.Instantiate("TestPlayer", spawnPosition, Quaternion.identity);
                this.character.name = this.character.name + idx.ToString();
                player.TagObject = this.character as GameObject;
                this.character.GetComponent<PlayerController>().isLocal = true;
                this.character.GetComponent<PlayerController>().Init();
               
                camController.Init(idx.ToString(), spawnPosition);
            }
            idx++;
        }
    }
    public override void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)
    {
        Debug.Log(PhotonNetwork.LocalPlayer.CustomProperties["IsRobber"]);
        Debug.Log(targetPlayer.CustomProperties["IsRobber"]);
        Debug.LogFormat("{0}, {1}", targetPlayer.NickName, changedProps["IsRobber"]);       
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
public class PlayerController : MonoBehaviourPun
{
    public Transform camera;
    public Rigidbody rigid;
    
    
    [HideInInspector]
    public bool isLocal;
    [SerializeField]
    float speed;
    VariableJoystick joystick;
           
    private void Start()
    {
        
    }
    public void Init()
    {
        if (!isLocal) return;
        speed = 3f;
        this.gameObject.GetComponent<CapsuleCollider>();
        var cameraGo = GameObject.Find("Main Camera");
        camera = cameraGo.GetComponent<Transform>();
        var joystickGo = GameObject.Find("Variable Joystick");
        joystick = joystickGo.GetComponent<VariableJoystick>();                
    }
    private void FixedUpdate()
    {
        if (!isLocal) return;
        Vector3 joystickDir = Vector3.forward * joystick.Vertical + Vector3.right * joystick.Horizontal;
        if (joystickDir == Vector3.zero) return;
        Vector3 playerAngle = Quaternion.LookRotation(joystickDir).eulerAngles;
        Vector3 camPivotAngle = camera.eulerAngles;
        Vector3 towardAngle = Vector3.up * (playerAngle.y + camPivotAngle.y);
        rigid.rotation = Quaternion.Euler(towardAngle);
        rigid.transform.Translate(Vector3.forward * Time.deltaTime * speed);
    }
    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Treasure")
        {
            Debug.Log("Triggered");
            Debug.Log(this.gameObject.name);
            var treasure = other.GetComponent<PhotonView>();
            PhotonNetwork.Destroy(treasure.gameObject);
            PhotonNetwork.LocalPlayer.CustomProperties.Add("IsRobber", PhotonNetwork.LocalPlayer.ActorNumber);                      
            PhotonNetwork.LocalPlayer.SetCustomProperties(PhotonNetwork.LocalPlayer.CustomProperties);
            Debug.Log(PhotonNetwork.NickName);
        }
    }
}
2번을 검증해보려고 했지만 계속 오류가 나서 일단 오늘은 여기서 접는다.
'Unity > Project : Cursed Treasure' 카테고리의 다른 글
| Unity 팀프 21.06.02 PUN2 - Client의 GameObject찾기 (0) | 2021.06.02 | 
|---|---|
| Unity 팀프 21.06.01 PUN2 - CustomProperties(해결)/RPC(R&D) (0) | 2021.06.02 | 
| Unity 팀프 21.05.29 PUN2 - CustomPropeties (0) | 2021.05.30 | 
| Unity 팀프 21.05.28 PUN2 - 조작, 개별스폰 (0) | 2021.05.29 | 
| Unity 팀프 21.05.27 Photon - 멀티구현 R&D (0) | 2021.05.28 |