top of page

Study] Photon Cloud(Unity)-17

  • 작성자 사진: 김영호
    김영호
  • 2023년 2월 7일
  • 1분 분량

최종 수정일: 2023년 4월 25일

  • 입장 순서에 따라 스폰 위치 바꾸기

지금까지는 Shooter의 스폰 위치는 Raptor는 왼쪽 Phontom은 오른쪽으로 고정 되어있었다.

하지만 플레이어가 같은 Shooter를 선택 할 수 있기 때문에 스폰 위치가 겹쳐지는 문제가 있기 때문에 이를 입장 순서에 따라 위치를 바꿔주도록 바꿔보겠다.


입장 순서는 "PhotonNetwork.Player.ActorNumber"를 사용하면 알아 낼 수 있다.

ActorNumber는 1부터 시작한다.


  • ActorNumber를 사용해 스폰 위치 나누기

...
using Photon.Pun;
using Photon.Realtime;

public class GameManager : MonoBehaviourPunCallbacks
{
    ...
    ...
    ...
    
    void Start()
    {
        //내가 선택한 슈터 이름
        string shooter = PhotonNetwork.LocalPlayer.CustomProperties["Shooter"].ToString();
        
        //Prefab name
        string prefabName = "Prefabs\\";
        if (shooter == "Raptor")        prefabName += raptor.name;
        else if (shooter == "Phantom")     prefabName += phantom.name;

        //Transform data
        Vector3 pos = transform.position + new Vector3(0f, -3f, 0f);
        //Player가 두명뿐이므로 ActorNumber가 1이면 왼쪽, 1이 아니라면 오른쪽
        pos += PhotonNetwork.LocalPlayer.ActorNumber == 1 ? Vector3.left : Vector3.right;

        //Instantiate
        PhotonNetwork.Instantiate(prefabName, pos, Quaternion.identity);
    }
}


  • Test



  • Game Score

public class GameManager : MonoBehaviourPunCallbacks
{
    ...
    ...
    ...
    
    void AddScore(int point)
    {
        score += point;
    }
}

GameManager에 int score를 추가하고 이를 증가시켜줄 함수를 만든다.


public class Ayako : Shooter
{
    ...
    ...
    ...
    
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Laser")
        {
            //총알 제거
            Destroy(other.gameObject);
            
            //Master일때만 동작
            if (PhotonNetwork.IsMasterClient)
            {
                hp -= 20f;

                if(hp <= 70)    //중간 데미지 Animation
                    animator.SetBool("MIDDLEDAMAGE", true);
                if(hp <= 30)    //높은 데미지 Animation
                    animator.SetBool("HIGHDAMAGE", true);
                if (hp <= 0)
                {
                    //Add Score
                    GameManager.instance.AddScore(10);
                    photonView.RPC("Dead", RpcTarget.All);
                }
            }
        }
    }
}

그리고 적기가 공격받아 죽을 때 Singleton 클래스인 GameManager를 호출해 AddScore() 함수로 score를 올려준다.

이 때 반드시 Master에서만 동작 하게 해주어서 한번의 점수를 중복으로 받는 것을 방지한다.



  • Test


  • Facebook
  • Twitter
  • LinkedIn

©2021 by 김영호_포트폴리오. Proudly created with Wix.com

bottom of page