3- 피하기 게임 만들기

2023. 2. 13. 10:53언어/유니티

728x90

플레이어와 배경을 배치해주고 

저기 보이는 hp 는 

UI->Image 를 클릭하여 캔버스 안에 source image 에 넣도록 한다.

 

 

화살 배치는 0 3.2 0 으로 해주고 prefab으로 만들어 준다.

prefab안에 이코드를 넣어준다,

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Arrowprefabs : MonoBehaviour
{
    public GameObject arrowprefabs;
    float span = 1.0f;
    float delta = 0;

    void Update()
    {
        this.delta += Time.deltaTime;
        //앞프레임과 현재 프레임사이의 시간차이를 deltaTime
        if (this.delta > this.span)
        {
            this.delta = 0;
            GameObject go=Instantiate(arrowprefabs) as GameObject;

            // Instantiate 복제하는것이다. 
            // as GameObject을 적는이유는 Instantiate는 object형을 반환
            //하지만 GameObject형을 받기위해서 강제형 변환을 해야된다
            //이럴때 이용하고 이것을 캐스트라고 부른다.
            
            int px = Random.Range(-6, 7);
            //-6부터 7까지 랜덤으로 
            go.transform.position = new Vector3(px, 7, 0);
            //랜덤으로 복제 
        }
    }
}

 

 

 

 

 

 

 

 

이제 플레이어 (고양이)에 

소스 코드를 넣어 줘야된다.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cat : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        { //키를 누른순간(왼쪽 화살표)
            transform.Translate(-3, 0, 0); 
        } // 포지션이 x축 -3이동 결국 왼쪽이동
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            transform.Translate(3, 0, 0);
        } 

    }
}

빈오브젝트 두개를 만들어준다.

이름은 이렇게 두개를 하겠다.

하나는 게임감독,하나는 화살 관리

라고 보면 좋을거 같다.

 

GameDirector에 이코드를 넣어준다,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Gamehp : MonoBehaviour
{
    GameObject hpGauge; 
    void Start()
    {
        this.hpGauge = GameObject.Find("hpGauge");
    } // hpgauge게임 오브젝트를 찾아서 hpgauge라고 칭함
    public void DecreaseHp()
    {
        this.hpGauge.GetComponent<Image>().fillAmount -= 0.1f;
    } 
    //DecreaseHp 함수 한개 만들어줘서 호출할수 있게끔 만듬
    //hpGauge에 있는 이미지에 속성을 준다. 
    //여기서의 속성 fillAmount 
    //원에서 조금씩 잘라내어 피가 없어진거 처럼 보여줌
}

 

이 체력바를 관리하는 코드이다

 

그리고 Filled 타입을 꼭 해줘야된다.

이 속성은 체력바 구현,스테미나,시간초 등등 많은 ui를 만드는데 이용한다.

다시 한번 보는게 좋을거같다.

 

 

Arrorprefabs 에는 이코드를 넣어준다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Arrow : MonoBehaviour
{
    GameObject player;
    void Start()
    {
        this.player = GameObject.Find("player");
        //게임 오브젝트중 player을 찾음
    }

    void Update()
    {
        transform.Translate(0, -0.1f, 0);
        // 매 프레임마다 -0.1f 씩 움직여서 떨어지는 것
        // 처럼 보임

        if (transform.position.y < -5.0f)
        { // y축 위치가 -5.0f 가되면 
            Destroy(gameObject); // 소멸시키는것 
            // 소멸 시켜주지 않으면 많은 메모리를 차지
        }

        Vector2 p1= transform.position; //화살 좌표
        Vector2 p2= this.player.transform.position; //플레이어 좌표
        Vector2 dir = p1 - p2; 
        float d= dir.magnitude; //삼각형의 크기
                            //피타고라스의 삼각형 윗변두개의 길이
        float r1 = 0.5f; //화살 반경
        float r2 = 1.0f; //플레이어 반경

        if (d < r1 + r2) 
            //화살 반경과 플레이어 반경보다 낮으면 
        {
            GameObject director = GameObject.Find("GameDirector");
            //충돌 했다는것을 표현하기 위해서 director라 해주고
            director.GetComponent<Gamehp>().DecreaseHp();
            //아까 만든 hp감소 함수를 가져온다.
            Destroy(gameObject);
            //맞으면 삭제
        }
    }
}

 

 

 


instantiate와  filled를 따로 공부 해줘야겠다.

 

 

728x90