게임오버가 나와벌여..

 

화살 프리팹 생성 위치 화면 안으로 변경

화면 밖으로 못나가게

체력 0이면 게임 오버 텍스트를 페이드인으로 출력

 (그래픽 수업 때 배운 3D 조명들도 언젠가 쓸 때가 있을 것)

 

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

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

    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>"); // 게임 오버
            this.gameover.SetActive(true); // 게임 오버 텍스트 활성화
        }

        float fillAmount = this.hp / this.maxHp;
        this.hpGauge.UpdateHP(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(-8.5f, 8.5f); // 랜덤 x좌표 

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

 

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

public class hpGauge : MonoBehaviour
{
    // 체력이 40 이하가 되면 이거 없애고 , 다른 오브젝트로 보여짐

    public Image hpUI;

    public void UpdateHP(float fillAmount)
    {
       // (맞았다고 보내주면 실행) 휠을 깎음
        this.hpUI.fillAmount = fillAmount;
    }
}

 

  • catGameover
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using static System.Net.Mime.MediaTypeNames;

public class catGameover : MonoBehaviour
{
   // Text text;
    TMP_Text tmpTxt;
 
    void Awake()
    {
        tmpTxt = GetComponent<TMP_Text>();

        StartCoroutine(coFadeIn());
        // StartCoroutine(coFadeOut());
    }

    IEnumerator coFadeIn() // 페이드인
    {
        tmpTxt.color = new Color(tmpTxt.color.r, tmpTxt.color.g, tmpTxt.color.b, 0); // 시작 시, 투명

        while (tmpTxt.color.a < 1.0f)
        {
            tmpTxt.color = new Color(tmpTxt.color.r, tmpTxt.color.g, tmpTxt.color.b, tmpTxt.color.a + (Time.deltaTime / 2.0f));
            yield return null;
        }
    }

    IEnumerator coFadeOut() // 페이드아웃
    {
        tmpTxt.color = new Color(tmpTxt.color.r, tmpTxt.color.g, tmpTxt.color.b, 1); 

        while (tmpTxt.color.a > 0f)
        {
            tmpTxt.color = new Color(tmpTxt.color.r, tmpTxt.color.g, tmpTxt.color.b, tmpTxt.color.a - (Time.deltaTime / 2.0f));
            yield return null;
        }
    }
}

 

페이드인- 아웃을 번갈아 실행하면 깜빡이게 됨

 

깜빡..

  • 깜빡이
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using static System.Net.Mime.MediaTypeNames;

public class catGameover : MonoBehaviour
{
   // Text text;
    TMP_Text tmpTxt;
 
    void Awake()
    {
        tmpTxt = GetComponent<TMP_Text>();

        StartCoroutine(coFadeIn());
        // StartCoroutine(coFadeOut());
    }

    IEnumerator coFadeIn() // 페이드인
    {
        tmpTxt.color = new Color(tmpTxt.color.r, tmpTxt.color.g, tmpTxt.color.b, 0); // 시작 시, 투명

        while (tmpTxt.color.a < 1.0f)
        {
            tmpTxt.color = new Color(tmpTxt.color.r, tmpTxt.color.g, tmpTxt.color.b, tmpTxt.color.a + (Time.deltaTime / 2.0f));
            yield return null;
        }
        StartCoroutine(coFadeOut());
    }

    IEnumerator coFadeOut() // 페이드아웃
    {
        tmpTxt.color = new Color(tmpTxt.color.r, tmpTxt.color.g, tmpTxt.color.b, 1); 

        while (tmpTxt.color.a > 0f)
        {
            tmpTxt.color = new Color(tmpTxt.color.r, tmpTxt.color.g, tmpTxt.color.b, tmpTxt.color.a - (Time.deltaTime / 2.0f));
            yield return null;
        }
        StartCoroutine(coFadeIn());
    }
}

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

밤송이 던지기1  (0) 2024.09.17
고양이 점프  (1) 2024.09.16
고양이 화살 피하기 2단계  (1) 2024.09.16
고양이 화살 피하기 1단계  (2) 2024.09.15
자동차 스와이프  (0) 2024.09.15

+ Recent posts