Unity 입문 팀프로젝트 - 1

이준호·2023년 11월 30일
0

📌 2D 입문 팀프로젝트

이번 팀 프로젝트는 고전게임을 리뉴얼 하는 주제가 주어졌다.

고전 게임으로 똥피하기, 닷지, 벽돌부수기 이렇게 3가지중 큰 주제를 정해서 만드는것 이었는데 우리는 똥피하기랑 비슷하지만 좀 다르게 옛 고전게임 "Pang"을 모티브로 만들기로 하였다.

일단 초반 역할 분담으로 '게임 시작 씬', '메인 화면 디자인', '공 오브젝트', '공격 시스템', '캐릭터' 이렇게 나누고 나는 공격 시스템을 하기로 하였다.

📌 Player Attack System

public class PlayerAttackSystem : MonoBehaviour
{
    // 플레이어가 공격버튼을 눌러 이벤트가 발돌되면 _controller에서 값( ) 을 받아옴
    // 플레이어의 transform(위치)를 기준으로 위쪽 방향으로 force만큼의 힘을 주며 Bullet Prefab을 찍어냄

    // 총알의 스크립트는 따로 작성 (총알 스크립트에서 충돌 처리)

    TopDownCharacterController _controller;

    Rigidbody2D bulletRigid;

    GameObject bullet; // ( bullet는 player가 발사하는 총알 Prefab )

    [SerializeField] int force = 5; // ( bullet에 가할 스피드 )
    [SerializeField] private float coolTime = 1.0f;

    private bool coolTimeCheck = true;

    private void Awake()
    {
        _controller = GetComponent<TopDownCharacterController>();
        bullet = Resources.Load<GameObject>("Prefabs/Bullet");
    }

    private void Start()
    {
        _controller.OnAttackEvent += Attack;
    }

    private void Attack()
    {
        // CoolTime Check
        if(coolTimeCheck == true) StartCoroutine(CollTime(coolTime));
    }

    private void RecallBullet()
    {
        GameObject playerBullet = Instantiate(bullet);
        playerBullet.transform.position = new Vector3(transform.position.x, transform.position.y + 0.4f, transform.position.z);
        ApplyAttck(playerBullet);
    }

    private void ApplyAttck(GameObject obj)
    {
        bulletRigid = obj.GetComponent<Rigidbody2D>();
        bulletRigid.AddForce(transform.up * this.force, ForceMode2D.Impulse);
    }

    private IEnumerator CollTime (float time)
    {
        // CoolTime Setting
        coolTimeCheck = false;

        RecallBullet();

        while (time > 0.0f)
        {
            time -= Time.deltaTime;
            yield return new WaitForFixedUpdate();
        }

        // CoolTime Reset
        coolTimeCheck = true;
    }
}

저번에 썻던것과 같은 동작으로 유저가 'Space'키를 누르면 이벤트가 발동되어 이 스크립트에 등록해둔 메서드가 실행되어 순차적으로 실행되어 총알을 플레이어 위로 발사하는 스크립트를 만들었다.

public class BulletPrefabSystem : MonoBehaviour
{
    // Bullet이 Ball(공)에 부딪힐때 로직
    // 공은 파괴 or 데미지를 준다.
    // Bullet은 Destory or SetActive(false)

    // Bullet이 Wall(벽) 에 부딪히면 Bullet은 Destory or SetActive(false) 

    private void OnCollisionEnter2D(Collision2D collision)
    {
        
    }
}

이제 내일은 플레이어가 쏜 총알을 공 오브젝트에 부딪힐때의 스크립트를 짜보려고 한다.

profile
No Easy Day

0개의 댓글