게임오버 디버깅까지 함

 

화살이 고양이를 감지하면 캣에게 데미지를 보내고

캣은 가지고 있던 체력에서 데미지만큼 깐다. 체력이 0이 되면 게임오버

체력 량을 ui에 반영되게 함

 

... 체력이 40이하면 다른 색 ui가 뜨게 하고 싶고, 게임창 밖으로 안나가게 할 거, 게임 오버, 화살 생성 위치..

public으로 게임오브젝트를 불러오는게 젤 직관적이고 쉽지만 프리팹화 등으로 작동이 안되는 경우가 있었다.

 

 - GameObject 이름; / 변수 이름 = GameObject.Find("이름"); / 변수 이름.GetComponent<이름>().매소드명();

 

스크립트도 게임 오브젝트로 불러올 수 있었다.

 

  • cat
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cat : MonoBehaviour
{
    public GameObject lButton;
    public GameObject rButton;

    public hpGauge hpGauge;

    float hp;
    float maxHp;

    private void Start()
    {
        this.hp = 0;
        this.maxHp = 100;
        hp =maxHp;
    }

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.LeftArrow))
        {
            this.transform.localScale = new Vector3(-1, 1, 1); // 방향에 따라 스프라이트 반전
            transform.Translate(-1, 0, 0);

            lButton.gameObject.SetActive(false); // 누르면 화살표 게임 오브젝트 비활성
        }
        if(Input.GetKeyDown(KeyCode.RightArrow))
        {
            this.transform.localScale = new Vector3(1, 1, 1);
            transform.Translate(1, 0, 0);

            rButton.gameObject.SetActive(false);
        }
        if (Input.GetKeyUp(KeyCode.LeftArrow))
            lButton.SetActive(true); // 떼면 활성

        if (Input.GetKeyUp(KeyCode.RightArrow))
            rButton.SetActive(true);
    }

    public void HitDamage(float damage) // 애로우에서 넘어온 매개변수
    {
        this.hp -= damage;
       //   Debug.Log($"피해를 받았습니다. {this.hp}/{this.maxHp}");

        if (this.hp <= 0)
        {
            Debug.Log($"<color=yellow>Game Over</color>"); // 게임 오버
        }

        float fillAmount = this.hp / this.maxHp;
        this.hpGauge.UpdateHP(fillAmount);
    }
}

 

  • hpGauge
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class hpGauge : MonoBehaviour
{
    public Image hpUI;

    public void UpdateHP(float fillAmount)
    {
       // (맞았다고 보내주면 실행) 받은 양으로 변환
        this.hpUI.fillAmount = fillAmount;
    }
}

 

  • arrow
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class arrow : MonoBehaviour
{
    public float arrowSpeed=0; // 0.01
    int damage;
    GameObject cat;

    private void Start()
    {
        this.cat = GameObject.Find("player"); // 신에서 이름이 플레이어인 게임 오브젝트를 찾는다.
        this.damage = 10;
    }

    void Update()
    {
        // 아래로 이동
        transform.Translate(0, -arrowSpeed, 0);

        // 화면 밖으로 나가면 화살표 삭제
        if(transform.position.y < -7)
        { 
            Destroy(this.gameObject); 
        }
    }

   // 고양이에 닿으면 화살표 삭제
   // 화살 콜라이더에 이즈트리거
   // 고양이한테 플레이어 태그
    public void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag == "Player")
        {
            cat.GetComponent<Cat>().HitDamage(this.damage); //플레이어가 가진 스크립트를 불러와 매서드에 매개 변수를 주어 넘긴다
            Destroy(this.gameObject);
        }
    }
}

 

  • arrowGenetator
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class arrowGenetator : MonoBehaviour
{
    public GameObject arrowPrafab;

    float spTime = 1.0f;
    float time=0f;

    void Update()
    {
        // -9.5, 9.5, 6 사이에 intantiate로 1초마다 애로우 프리팹 랜덤 생성
        this.time += Time.deltaTime; // 생성 시간 카운트
       // Debug.Log(time);

        if (this.spTime < this.time) // 1초마다
        {
            this.time = 0f; // 생성 시간 리셋
            this.CreateArrow();
            this.CreateArrow();
        }
    }

    void CreateArrow()
    {
        GameObject arGo = Object.Instantiate(arrowPrafab); // 프리팹 생성

        float posX = Random.Range(-9.5f, 9.5f); // 랜덤 x좌표 

        arGo.transform.position = new Vector3(posX, 6, 0); // 프리팹 생성 포지션 결정
    }
}

'└▶연습' 카테고리의 다른 글

고양이 점프  (0) 2024.09.16
고양이 화살 피하기 3단계  (6) 2024.09.16
고양이 화살 피하기 1단계  (2) 2024.09.15
자동차 스와이프  (0) 2024.09.15
룰렛 기본형  (0) 2024.09.15

+ Recent posts