상세 컨텐츠

본문 제목

유니티 총 타입 별로 총알 구분하기

유니티/기능

by MJ_119 2024. 9. 16. 00:54

본문

- 총 종류별로 총알 구분하기

( 돌격소총, 저격총, 로켓 등 )

 

 

총 쏠때 AmmoType을 확인해서 어떤 타입의 총알을 사용하는지 확인.

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

public class RayCast : MonoBehaviour
{
    [SerializeField] Camera mainCam;
    [SerializeField] float range = 100f;
    [SerializeField] float gunDamage = 30f;
    [SerializeField] ParticleSystem gunFlash;
    [SerializeField] GameObject gunTargetHit;
    [SerializeField] Ammo ammo; // 플레이어가 가지고있는 총알 확인
    [SerializeField] AmmoType ammoType;
    [SerializeField] float weaponDelayTime;

    public bool canShoot = true;

    private void OnEnable()
    {
        canShoot = true;
    }

    void Update()
    {
        if(Input.GetMouseButtonDown(0) && canShoot == true)
        {
            if (ammo.GetCurrentAmmo(ammoType) > 0)
            {
                
                StartCoroutine(Shoot());
                
            }
        }
    }

    void GunTargetHit(RaycastHit raycastHit)
    {
        // 총 맞은 부위에 이펙트 생성
        //GameObject hitEffect = Instantiate(gunTargetHit, raycastHit.transform);
        GameObject hitEffect = Instantiate(gunTargetHit, raycastHit.point, Quaternion.LookRotation(raycastHit.normal));

        // 1초뒤 파티클 삭제
        Destroy(hitEffect, 1f);
    }

    void GunFlash()
    {
        gunFlash.Play();
    }

    IEnumerator Shoot()
    {
        canShoot = false;

        GunFlash();
        ProcessRayCast();
        ammo.ReduceCurrentAmmo(ammoType);

        yield return new WaitForSeconds(weaponDelayTime);

        canShoot = true;
    }

    private void ProcessRayCast()
    {
        RaycastHit hit;
        if (Physics.Raycast(mainCam.transform.position, mainCam.transform.forward, out hit, range))
        {
            print(" hit : " + hit.transform.name);

            // 총 맞은 곳에 파티클 생성
            GunTargetHit(hit);

            //Raycast로 충돌한 오브젝트에서 EnemyHealth라는 컴포넌트를 찾아서, 그 컴포넌트를 가져옴.
            EnemyHealth target = hit.transform.GetComponent<EnemyHealth>();

            if (target == null)
            {
                return;
            }

            target.TakeDamage(gunDamage);
            print(target.Hp);
        }
    }
}

 

 

 

- 그다음 AmmoSlot GetAmmoSlot(AmmoType ammoType)을 통해서 알맞은 slot을 return받고 그걸 처리한다.

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

public class Ammo : MonoBehaviour
{
    [SerializeField] AmmoSlot[] ammoSlots;

    [Serializable]
    private class AmmoSlot
    {
        public AmmoType ammoType;
        public int ammoAmount;
    }


    public int GetCurrentAmmo(AmmoType ammoType)
    {
        return GetAmmoSlot(ammoType).ammoAmount;
    }
    public void ReduceCurrentAmmo(AmmoType ammoType)
    {
        GetAmmoSlot(ammoType).ammoAmount--;
    }

    private AmmoSlot GetAmmoSlot(AmmoType ammoType)
    {
        foreach(AmmoSlot slot in ammoSlots)
        {
            if (slot.ammoType == ammoType)
                return slot;
        }
        return null;
    }
}

 

관련글 더보기