[Unity] 2D 팀 프로젝트 제작 내용

고현규·2023년 12월 5일

만든 내용

  1. 공이 좌 혹은 우로 이동
  2. 총알에 맞으면 공이 작은 공 둘로 분리
  3. 공이 부셔지면 확률적으로 무기 아이템이 드랍

Pang!을 기반으로 한 게임을 만들었다.
나는 피격되면서, 공격해야하는 공을 생성하고
공을 프리팹 화 시켜서 3가지 종류의 프리팹을 넣고
모두 같은 cs 파일을 가지고 있다.
데미지와 체력을 각 프리팹에 적용시켰다.

공이 부셔지면 확률적으로 무기 아이템이 드랍된다.


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

public class BounceBall : MonoBehaviour
{
    [SerializeField] private int _ballHp;
    [SerializeField] private int _ballDamage;
    [SerializeField] private float speed = 300;

    private Hearts _heart;
    private Animator _anim;

    private Rigidbody2D _rigidbody;

    Rigidbody2D SmallBallRigid;

    public List<GameObject> BallList;
    public List<GameObject> RewardItemList;

    private void Awake()
    {
        _rigidbody = GetComponent<Rigidbody2D>();
    }

    private void Start()
    {
        if (_ballHp == 3)
            RandomLaunch(this.gameObject);

        _heart = GameObject.Find("Player").GetComponent<Hearts>();
        _anim = GameObject.Find("Player").GetComponent<Animator>();

    }

    // 왼쪽, 오른쪽 랜덤한 값을 받아 해당 방향으로 Speed만큼 곱한 속도로 이동
    private void RandomLaunch(GameObject obj)
    {
        float x = Random.Range(0, 2) == 0 ? -1 : 1;
        Vector2 dir = new Vector2(x, 0);
        _rigidbody.AddForce(dir * speed);
    }

   
    // 매개변수 방향대로 공이 이동
    private void DirtionLaunch(GameObject obj, Vector2 direction)
    {
        SmallBallRigid = obj.GetComponent<Rigidbody2D>();
        SmallBallRigid.AddForce(direction * speed);
    }

    // x: -8 ~ 8 /  y: 2 ~ 3 사이 위치로 랜덤하게 변경
    public void RandomSpawn(GameObject obj)
    { 
        Vector2 RandomPos = new Vector2(Random.Range(-8, 8), Random.Range(2, 3));
        this.transform.position = RandomPos;
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Bullet")
        {
            Debug.Log("공에 총알이 맞았습니다.");
            BallHitted();
            Destroy(gameObject);
        }
        else if (collision.gameObject.tag == "Player")
        {

            _heart.DecreaseHealth(_ballDamage); // 체력 감소
            if (_heart.playerHealth <= 0)
                _anim.SetTrigger("Death");

            Debug.Log($"플레이어가 공에 맞아 피가 {_ballDamage}만큼 깎였습니다.");
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Bullet")
        {
            Debug.Log("공에 총알이 맞았습니다.");
            BallHitted();
            Destroy(gameObject);
        }
    }

    private void OnDestroy()
    {
        int RandomPercent = Random.Range(0, 5);
        if (RandomPercent == 1)
        {
            int RandomIndex = Random.Range(0, 5);
            Instantiate(RewardItemList[RandomIndex], this.transform.position, Quaternion.identity);
        }
    }

    private void BallHitted()
    {
        if (_ballHp == 3)
            Split(1);
        else if(_ballHp == 2) 
            Split(2);
        else
            Destroy(gameObject);
    }

    private void Split(int index)
    {
        GameObject ball1 = Instantiate(BallList[index], new Vector3(transform.position.x - 0.2f, transform.position.y, transform.position.z), Quaternion.identity);
        GameObject ball2 = Instantiate(BallList[index], new Vector3(transform.position.x + 0.2f, transform.position.y, transform.position.z), Quaternion.identity);
        DirtionLaunch(ball1, Vector2.left);
        DirtionLaunch(ball2, Vector2.right);
    }
}
profile
게임 개발과 기획

0개의 댓글