본문 바로가기

유니티/수업 내용

CatEscape 화살피하기

hp가 0일때 화살을 더이상 만들지 않기

 

1. 레바를 만들어서 hp가 0이면 공장작동중지시키기

GameDirector

    public void DecreaseHp()
    {
        Image image = this.hpGauge.GetComponent<Image>();
        image.fillAmount -= 0.1f;

        if (image.fillAmount == 0)
        {
            GameObject arrowGeneratorGo = GameObject.Find("ArrowGenerator");
            ArrowGenerator arrowGenerator = arrowGeneratorGo.GetComponent<ArrowGenerator>();
            arrowGenerator.isAllive = false;
        }
    }

ArrowGenerator 화살공장

    public GameObject arrowPrefab;
    float span = 1.0f;
    float delta = 0;
    public bool isAllive = true;

    // Update is called once per frame
    void Update()
    {
        this.delta += Time.deltaTime;   //매 프레임마다 경과시간을 누적
        if (this.delta > this.span && isAllive) //1초보다 넘어갔고 살아있다면
        {
            //누적시간 초기화
            this.delta = 0;
            //프리팹의 인스턴스를 생성한다
            GameObject go = Instantiate(this.arrowPrefab) as GameObject;
            int px = Random.Range(-6, 7);   //-6 ~ 6사이 x좌표
            go.transform.position = new Vector3(px, 7, 0);
        }
    }

 


 

2. hp가 0이면 아예 공장을 파괴시켜버리기

 

 

GameDirector

 

ArrowGenerator 화살공장

 

 


캐릭터를 움직일때 화면밖으로 나가지 않도록하기

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            this.transform.Translate(-3, 0, 0);
            if (this.transform.position.x < -8.38f)
            {
                this.transform.position = new Vector3(-8.38f, -3.64f, 0);
            }
        }

        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            this.transform.Translate(3, 0, 0);
            if (this.transform.position.x > 8.44f)
            {
                this.transform.position = new Vector3(8.44f, -3.64f, 0);
            }
        }
    }