상세 컨텐츠

본문 제목

유니티 공부(6)

카테고리 없음

by MJ_119 2023. 8. 30. 00:55

본문

출처 : 골드메탈 유니티 뱀서라이크 07

 

근접 무기 구현

 

1. 프리펩 만들기

 

- box 콜라이더 2D 추가후 Is trigger 체크!

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

public class Bullet : MonoBehaviour
{
    public float damage;
    public int per;

    public void Init(float damage, int per)
    {
        this.damage = damage; // this.damage는 위에 지정한 damage, 오른쪽에 damage는 Init(damage)를 뜻함.
        this.per = per;
    }
}

 

 

2. 충돌 로직 작성

 - Enemy 스크립트

void OnTriggerEnter2D(Collider2D collision)
    {
        if (!collision.CompareTag("Bullet"))
            return;

        health -= collision.GetComponent<Bullet>().damage;

        if (health > 0)
        {
            // 아직 살아있음 , Hit action
        }
        else
        {
            // 몬스터 사망.. Dead 애니메이션
            Dead();
        }

        void Dead()
        {
            gameObject.SetActive(false);
        }
    }

 

3. 근접 무기 배치

 - Weapon 스크립트

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

public class Weapon : MonoBehaviour
{
    // 무기 id, 프리팹 id, 데미지, 개수, 속도
    public int id;
    public int prefabId;
    public float damage;
    public int count; 
    public float speed;
    

    void Start()
    {
        Init();
    }

    void Update()
    {
        
    }

    public void Init()
    {
        switch (id)
        {
            case 0:
                speed = -150;
                Batch();
                break;
            default:
                break;
        }
    }

    void Batch() // 생성된 무기를 배치하는 함수
    {
        for ( int index = 0; index < count; index++ )
        {
            Transform bullet = GameManager.instance.pool.Get(prefabId).transform;
            bullet.parent = transform; // 부모 오브젝트 변경
            bullet.GetComponent<Bullet>().Init(damage, -1); // -1는 무한으로 관통한다는 뜻
        
        }
    }
}

 

 

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

public class Weapon : MonoBehaviour
{
    // 무기 id, 프리팹 id, 데미지, 개수, 속도
    public int id;
    public int prefabId;
    public float damage;
    public int count; 
    public float speed;
    

    void Start()
    {
        Init();
    }

    void Update()
    {
        switch (id)
        {
            case 0:
                transform.Rotate(Vector3.back * speed * Time.deltaTime);
                break;
            default:
                break;
        }
    }

    public void Init()
    {
        switch (id)
        {
            case 0:
                speed = -150;
                Batch();
                break;
            default:
                break;
        }
    }

    void Batch() // 생성된 무기를 배치하는 함수
    {
        for ( int index = 0; index < count; index++ )
        {
            Transform bullet = GameManager.instance.pool.Get(prefabId).transform;
            bullet.parent = transform; // 부모 오브젝트 변경

            Vector3 rotVec = Vector3.forward * 360 * index / count; // 무기 회전 로직
            bullet.Rotate(rotVec);

            bullet.Translate(bullet.up * 1.5f, Space.World);
            //bullet.Translate(bullet.up * 1.5f, Space.Self);

            bullet.GetComponent<Bullet>().Init(damage, -1); // -1는 무한으로 관통한다는 뜻
        
        }
    }
}

 

 

4. 레벨에 따른 배치

 - Weapon 스크립트

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

public class Weapon : MonoBehaviour
{
    // 무기 id, 프리팹 id, 데미지, 개수, 속도
    public int id;
    public int prefabId;
    public float damage;
    public int count; 
    public float speed;
    

    void Start()
    {
        Init();
    }

    void Update()
    {
        switch (id)
        {
            case 0:
                transform.Rotate(Vector3.back * speed * Time.deltaTime); // speed가 -150이기 때문에 Vector3.back을 사용.
                break;
            default:
                break;
        }

        if (Input.GetButtonDown("Jump"))
        {
            LevelUp(10,1);
        }
    }

    public void LevelUp(float damage, int count)
    {
        this.damage = damage;
        this.count += count;

        if (id == 0)
            Batch();
    }

    public void Init()
    {
        switch (id)
        {
            case 0:
                speed = -150;
                Batch();
                break;
            default:
                break;
        }
    }

    void Batch() // 생성된 무기를 배치하는 함수
    {
        for ( int index = 0; index < count; index++ )
        {

            Transform bullet;

            if (index < transform.childCount) // 자신의 자식 오브젝트 개수 확인은 childCount 속성.
            {
                bullet = transform.GetChild(index);
            }
            else
            {
                bullet = GameManager.instance.pool.Get(prefabId).transform;
                bullet.parent = transform; // 부모 오브젝트 변경
            }
            
            //배치 하면서 먼저 위치,회전 초기화하기!
            bullet.localPosition = Vector3.zero; // 위치 초기화
            bullet.localRotation = Quaternion.identity; // 회전 초기화

            Vector3 rotVec = Vector3.forward * 360 * index / count; // 무기 회전 로직
            bullet.Rotate(rotVec);

            bullet.Translate(bullet.up * 1.5f, Space.World);
            //bullet.Translate(bullet.up * 1.5f, Space.Self);

            bullet.GetComponent<Bullet>().Init(damage, -1); // -1는 무한으로 관통한다는 뜻
        
        }
    }
}