본문 바로가기
유니티 기초

무기 바꿔끼우기

by 노재두내 2023. 8. 11.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Test4_EquipItem
{
    public class HeroController : MonoBehaviour
    {
        //[SerializeField]
        //public GameObject weaponGo;//착용중인 무기 게임오브젝트 
        [SerializeField]
        private Transform weaponTrans;//히어로 프리팹의 자식 루트의 자식 pelvis의 자식 Weapon trans(sword의 부모)
        [SerializeField]
        private Transform shieldTrans;
        public Transform ShieldTrans
        {
            get
            {
                return this.shieldTrans;
            }
        }
        public Transform WeaponTrans
        {
            get
            {
                return this.weaponTrans;
            }
        }
        public bool HasWeapon()
        {
            return this.weaponTrans.childCount > 0;
        }
        public bool HasShield()
        {
            return this.shieldTrans.childCount > 0;
        }
        public void unEquipWeapon()
        {
            //if (this.weaponGo != null)
            //{
            //    Destroy(this.weaponGo);
            //}
            //else
            //{
            //    Debug.Log("착용중인 무기가 없습니다.");
            //}
            Debug.LogFormat("자식의 수:{0}", this.weaponTrans.childCount);
            if (this.weaponTrans.childCount == 0)
            {
                //착용중인 무기가 없다 .
                Debug.Log("착용중인 무기가 없습니다.");

            }
            else
            {
                //착용중인 무기가 있다
                Transform child =this.weaponTrans.GetChild(0);//첫번째 자식 
                //무기를 제거
                Destroy(child.gameObject);
            }
        }
        //방패 제거 
        public void unEquipShield()
        {
            if (this.shieldTrans.childCount == 0)
            {
                Debug.Log("착용중인 방패가 없습니다.");
            }
            else
            {
                Transform child = this.shieldTrans.GetChild(0);
                Destroy(child.gameObject);
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Test_EquipItemMain : MonoBehaviour
{
    [SerializeField]
    private Button btnRemoveSword;
    [SerializeField]
    private Button btnEquipSword0;//생성시 부모지정
    [SerializeField]
    private Button btnEquipSword1;//생성 후 부모지정


    [SerializeField]
    private GameObject swordPrefab;
    [SerializeField]
    private Test4_EquipItem.HeroController heroController;


    [SerializeField]
    private Button btnRemoveShield;
    [SerializeField]
    private Button btnEquipShield0;//생성 시 부모 지정
    [SerializeField]
    private Button btnEquipShield1;//생성 후 부모지정

    [SerializeField]
    private GameObject shieldPrefab;
    // Start is called before the first frame update
    void Start()
    {
        this.btnRemoveSword.onClick.AddListener(() =>
        {
            Debug.Log("영웅에게서 칼이 있다면 신에서 제거");
            //무기를 제거
            this.heroController.unEquipWeapon();
        });

        this.btnEquipSword0.onClick.AddListener(() =>
        {
            Debug.Log("생성시 부모를 지정");
            //새롭게 장착할
            bool hasWeapon= this.heroController.HasWeapon();
            if (!hasWeapon)
            {
                Instantiate(this.swordPrefab, this.heroController.WeaponTrans);
            }
            else
            {
                Debug.Log("이미 착용중입니다.");
            }
        });

        this.btnEquipSword1.onClick.AddListener(() =>
        {
            Debug.Log("생성 후 부모를 지정");
            bool hasWeapon = this.heroController.HasWeapon();
            if (!hasWeapon)
            {
                GameObject go = Instantiate(this.swordPrefab);
                //부모를 지정
                go.transform.SetParent(this.heroController.WeaponTrans);
                //위치를 초기화
                go.transform.localPosition = Vector3.zero;
                //회전을 초기화
                go.transform.localRotation = Quaternion.identity;
            }
            else
            {
                Debug.Log("이미 착용중입니다.");
            }
        });



        //--------------------------방패 착용 제거----------------------------------------------
        this.btnRemoveShield.onClick.AddListener(() =>
        {
            Debug.Log("방패 제거");
            this.heroController.unEquipShield();
        });

        this.btnEquipShield0.onClick.AddListener(() =>
        {
            Debug.Log("생성 시 방패 착용");
            bool hasShield = this.heroController.HasShield();
            if (!hasShield)//방패 있냐? 
            {
                //방패 없다
                Instantiate(this.shieldPrefab, this.heroController.ShieldTrans);
            }
            else
            {
                //방패 있다.
                Debug.Log("이미 방패 착용중입니다.");
            }
        });
        this.btnEquipShield1.onClick.AddListener(() =>
        {
            Debug.Log("생성 후 방패 착용");
            bool hasShield = this.heroController.HasShield();
            if (!hasShield)
            {
                GameObject shieldGo= Instantiate(this.shieldPrefab);
                shieldGo.transform.SetParent(this.heroController.ShieldTrans);
                shieldGo.transform.localPosition= Vector3.zero;
                shieldGo.transform.localRotation = Quaternion.identity;
            }
            else
            {
                Debug.Log("이미 방패 착용중입니다.");
            }
        });
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

shieldGo.transform.localRotation = Quaternion.Euler(new Vector3(-0.106f,-104.917f,0.077f));

 

방패 회전이 어색해서 수정

--------------------------------------------방패 바닥에 버리기-------------------------------------------

setParent(null)?하면 바닥에 버림 

 

remove 쉴드 버튼 누르면 버리기

public void unEquipShield()
        {
            if (this.shieldTrans.childCount == 0)
            {
                Debug.Log("착용중인 방패가 없습니다.");
            }
            else
            {
                Transform child = this.shieldTrans.GetChild(0);
                child.transform.SetParent(null);
                child.transform.localPosition = new Vector3(child.transform.position.x, 0, child.transform.position.z);
                child.transform.localRotation = Quaternion.Euler(new Vector3(0, 90, -90));
                //Destroy(child.gameObject);
            }
        }

'유니티 기초' 카테고리의 다른 글

주말과제 (simple rpg)(수정)  (1) 2023.08.14
rpg몬스터 여태한거 합치기  (0) 2023.08.11
포탈 한번만 나오기 수정 & 아이템먹기  (0) 2023.08.10
히어로 이동  (0) 2023.08.10
몬스터 죽으면 아이템 생성  (0) 2023.08.10