고양이가 바라보는 방향으로 스프라이트 반전

방향 키를 누르면  화살표가 깜빡임

화살표와 고양이 충돌 체크

> 고양이 체력 깍기.. 해야됨

 

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

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

    int hp;
    int 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);
    }
}

 

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

public class arrow : MonoBehaviour
{
    public float arrowSpeed=0; // 0.01
    bool isCat;

    public GameObject UIContr;

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

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

   // 고양이에 닿으면 화살표 삭제
   // 화살 콜라이더에 이즈트리거
   // 고양이한테 플레이어 태그
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag == "Player")
        {
            Destroy(this.gameObject);
            Debug.Log("맞음");
        }
    }
}

 

  • 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); // 프리팹 생성 포지션 결정
    }
}

 

  • 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;
    }
}

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

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

+ Recent posts