22.01.18 Cat Escape 피하기 게임 만들기

2022. 1. 18. 17:33Unity3D/수업 과제

 

실행 결과

 

소스코드------------------------------------------------------------------------------------------

 

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

public class PlayerController : MonoBehaviour
{
    private float xRange;
    private float yPos;
    public float hp;
    private bool moveLeft = false;
    private bool moveRight = false;

    void Start()
    {
        yPos = -3.5f;
        xRange = 8.5f;
        hp = 1f;
    }

    public void DecreaseHP()
    {
        hp -= 0.1f;
    }

    void FixedUpdate()
    {
        if(this.gameObject.transform.position.x > xRange)
        {
            this.gameObject.transform.position = new Vector2(xRange, yPos);
        }else if(this.gameObject.transform.position.x < -xRange)
        {
            this.gameObject.transform.position = new Vector2(-xRange, yPos);
        }

        if (moveLeft)
        {
            MoveLeft();
        }
        if (moveRight)
        {
            MoveRight();
        }
    }

    public void MoveLeft()
    {
        this.gameObject.transform.Translate(new Vector3(-10f * Time.fixedDeltaTime, 0f, 0f));
    }
    
    public void MoveRight()
    {
        this.gameObject.transform.Translate(new Vector3(10f * Time.fixedDeltaTime, 0f, 0f));
    }

    public void LeftPointerDown()
    {
        moveLeft = true;
    }

    public void LeftPointerUp()
    {
        moveLeft = false;
    }

    public void RightPointerDown()
    {
        moveRight = true;
    }

    public void RightPointerUp()
    {
        moveRight = false;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ArrowController : MonoBehaviour
{
    private GameObject player;

    void Start()
    {
        player = GameObject.Find("player");
    }

    void Update()
    {
        if (!GameManager.isOver)
        {
            this.transform.Translate(Vector3.down * 10f * Time.deltaTime);

            if (this.transform.position.y <= -4.5f)
            {
                GameManager.score += 10;
                Destroy(this.gameObject);
            }

            Vector2 p1 = this.transform.position;
            Vector2 p2 = this.player.transform.position;

            float di = Vector2.Distance(p1, p2);

            if (di < 1.5f)
            {
                Destroy(this.gameObject);
                player.GetComponent<PlayerController>().DecreaseHP();
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ArrowGenerator : MonoBehaviour
{
    private float timer;
    private float gnr;
    public GameObject arrow;
    private float xRange;
    private float yPos;

    void Start()
    {
        yPos = 6f;
        xRange = 8.5f;
        timer = 0f;
        gnr = 0.1f;
    }

    void Update()
    {
        if (!GameManager.isOver)
        {
            timer += Time.deltaTime;

            if (timer > gnr)
            {
                timer = 0f;
                Vector2 pos = new Vector2(Random.Range(-xRange, xRange), yPos);
                Instantiate<GameObject>(arrow, pos, arrow.transform.rotation);
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    private PlayerController player;
    private GameObject text;
    public static bool isOver = false;
    [SerializeField] private float timer;
    public static int score;
    [SerializeField] private Image hp_gauge;

    void Start()
    {
        score = 0;
        player = FindObjectOfType<PlayerController>();
        text = GameObject.Find("Text");
    }

    void Update()
    {
        timer -= Time.deltaTime;
        hp_gauge.fillAmount = player.hp;

        if(timer <= 0f || player.hp <= 0f)
        {
            isOver = true;
        }
        if (isOver)
        {
            text.GetComponent<Text>().text = "게임 오버" + "\n점수 : " + score;
            player.gameObject.SetActive(false);
        } else
        {
            text.GetComponent<Text>().text = "남은 시간 : " + (int)timer + "\n점수 : " + score;
        }
    }
}

'Unity3D > 수업 과제' 카테고리의 다른 글

22.02.09 유저 인터페이스 완성하기  (0) 2022.02.09
22.01.26 논스톱 나이트 모작하기  (0) 2022.01.26
22.01.20 MiniTest  (0) 2022.01.20
22.01.19 Cat Escape Two  (0) 2022.01.19
22.01.17 [홈워크] 표창 던지기  (0) 2022.01.17