상세 컨텐츠

본문 제목

유니티 뉴 인풋 시스템 무기 교체, 드랍(버리기), 픽업(획득)

유니티/기능

by MJ_119 2024. 10. 15. 23:30

본문

- 유니티 뉴 인풋 시스템 무기 교체, 드랍(버리기)

 

- EquipWeapon(0), EquipWeapon(1) 함수를 통해 무기를 교체한다.

- DropWeapon()을 통해 무기를 버린다. (무기 리스트에서 제거)

 

- Weapon.cs

public enum WeaponType
{
    Pistol,
    Revolver,
    AutoRifle,
    Shotgun,
    Rifle
}



[System.Serializable]
public class Weapon
{
    public WeaponType weaponType;
    public int ammo;
    public int maxAmmo;
}

 

- PlayerWeaponControllers.cs

using System.Collections.Generic;
using UnityEngine;

public class PlayerWeaponControllers : MonoBehaviour
{
    const float REFERENCE_BULLET_SPEED = 10f;

    Player player;

    [SerializeField] Weapon currentWeapon;
    [SerializeField] int maxSlots = 2;

    [Header("bullet Details")]
    [SerializeField] GameObject bulletPrefab;
    [SerializeField] float bulletSpeed;
    [SerializeField] Transform gunPoint;

    [SerializeField] Transform weaponHolder;
    [SerializeField] Transform aim;

    [Header("Inventory")]
    [SerializeField] List<Weapon> weaponSlots;

    void Start()
    {
        player = GetComponent<Player>();
        AssignInputEvents();

        currentWeapon.ammo = currentWeapon.maxAmmo;
    }


    void AssignInputEvents()
    {
        PlayerControlls controls = player.controls;

        controls.Character.Fire.performed += context => Shoot();

        controls.Character.EquipSlot1.performed += context => EquipWeapon(0);
        controls.Character.EquipSlot2.performed += context => EquipWeapon(1);

        controls.Character.DropCurrentWeapon.performed += context => DropWeapon();

    }

    void EquipWeapon(int i)
    {
        currentWeapon = weaponSlots[i];
    }

    void DropWeapon()
    {
        if ( weaponSlots.Count <= 1 )
        {
            return;
        }

        weaponSlots.Remove(currentWeapon);

        currentWeapon = weaponSlots[0];
    }

    public void PickupWeapon(Weapon newWeapon)
    {
        if (weaponSlots.Count >= maxSlots)
        {
            return;
        }

        weaponSlots.Add(newWeapon);
    }

    private void Shoot()
    {
        if( currentWeapon.ammo <= 0 )
        {
            return;
        }

        currentWeapon.ammo--;

        GameObject newBullet = Instantiate(bulletPrefab, gunPoint.position, Quaternion.LookRotation(gunPoint.forward));

        Rigidbody rbNewBullet = newBullet.GetComponent<Rigidbody>();

        rbNewBullet.mass = REFERENCE_BULLET_SPEED / bulletSpeed;
        rbNewBullet.velocity = BulletDirection() * bulletSpeed;

        Destroy(newBullet, 5f);

        GetComponentInChildren<Animator>().SetTrigger("Fire");
    }

    Vector3 BulletDirection()
    {
        Vector3 direction = (aim.position - gunPoint.position).normalized;

        if(player.aim.CanAimPrecisely() == false && player.aim.Target() == null)
        {
            direction.y = 0;
        }
        

        gunPoint.LookAt(aim.position);


        return direction;
    }

    //private void OnDrawGizmos()
    //{
    //    Gizmos.DrawLine(weaponHolder.position, weaponHolder.position + weaponHolder.forward * 25);
    //    Gizmos.color = Color.yellow;
    //    Gizmos.DrawLine(gunPoint.position, gunPoint.position + BulletDirection() * 25);
        
    //}
}

 

- Input system.

 

 

- Item_Pickup.cs

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

public class Item_Pickup : MonoBehaviour
{
    [SerializeField] Weapon weapon;

    private void OnTriggerEnter(Collider other)
    {
        other.GetComponent<PlayerWeaponControllers>()?.PickupWeapon(weapon);
    }
}

 

 

 

- 아이템에 픽업 스크립트, 박스콜라이더를 넣고, 아이템에 다가가면 픽업 함수 실행.

 

    public void PickupWeapon(Weapon newWeapon)
    {
        if (weaponSlots.Count >= maxSlots)
        {
            return;
        }

        weaponSlots.Add(newWeapon);
    }

관련글 더보기