top of page

Study] Photon Cloud(Unity)-14

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

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

  • Enemy Spawn

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

public class Spawner : MonoBehaviour
{
    //Spawn target
    public GameObject enemy;

    //Unity Functions
    void Start()
    {
        //코루틴을 사용해서 스폰을 반복함
        StartCoroutine(Spawn());
    }

    //Coroutine function for Enemy spawn
    IEnumerator Spawn()
    {
        while(true)
        {
            //마스터일때 적기를 포톤네트워크로 Instantiate를 함
            if (PhotonNetwork.IsMasterClient)
            {
                string prefabName = "Prefabs\\" + enemy.name;  //Prefab 경로
                Vector2 pos = transform.position;              //생성 위치

                //적기 스폰
                PhotonNetwork.Instantiate(prefabName, pos, Quaternion.identity);
            }

            //10초 대기
            yield return new WaitForSeconds(10f);
        }
    }
}

적군은 마스터에서 실제로 생성되고, 나머지 클라이언트들은 Photon Cloud를 통해 마스터쪽에 생성되어있는 적군을 보게 한다.


  • Enemy Shooter(Ayako)

public class Ayako : Shooter
{
    //State
    enum State { DOWN, LEFT, RIGHT }
    State state = State.DOWN;

    float yOffset = 0;
    
    protected override void Start()
    {
        //GetComponent<PhotonView>()
        base.Start();
        
        //Enemy가 처음 내려오는 거리
        yOffset = Random.Range(-1f, 1f);
    }
    
    void Update()
    {
        Move();
    }
    
    protected override void Move()
    {
        Vector2 direction = Vector2.zero;
        switch (state)
        {
            case State.DOWN:    //아래
                direction = Vector2.down;

                //Down limit 2f
                if (transform.position.y <= 2.0f + yOffset)
                    state = State.Left;
                break;                
            case State.LEFT:    //왼쪽
                direction = Vector2.left;

                //Left limit -1.7f
                if (transform.position.x <= -1.7f)
                    state = State.Right;
                break;                
            case State.RIGHT:    //오른쪽
                direction = Vector2.right;

                //Right limit 1.7f
                if (transform.position.x >= 1.7f)
                    state = State.Left;
                break;
        }
    
        //이동
        transform.Translate(direction * speed * Time.deltaTime);
    }
}

Enemy는 일정 높이까지 내려온뒤 좌우로 움직인다.



  • Test

ree

현재 yOffset을 Random으로 받아와 내려오는 높이를 다양하게 구성했는데, 지금 이 상태에서는 각 클라이언트마다 랜덤값이 다르게 되어 똑같이 보이지 않는 문제가 있다.

이는 다음 게시글에서 해결하겠다.

  • Facebook
  • Twitter
  • LinkedIn

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

bottom of page