728x90

https://docs.unity3d.com/kr/2019.4/Manual/RandomNumbers.html

 

랜덤 게임플레이 요소 추가 - Unity 매뉴얼

여러 게임에서 임의로 선택되는 항목 및 값은 중요합니다. 이 섹션에서는 Unity에 내장된 랜덤 함수를 사용하여 일반적인 게임 역학을 구현하는 방법에 대해 알아봅니다.

docs.unity3d.com

이 글을 참고하여 스크립트를 작성 하였으며

 

public void ItemDrop()
    {

        Choose(new float[3] { 10f, 10f, 80f }); // 퍼센트 조정 10 10 80 
        float Choose(float[] probs)
        {

            float total = 0;

            foreach (float elem in probs)
            {
                total += elem;
            }

            float randomPoint = Random.value * total;

            for (int i = 0; i < probs.Length; i++)
            {
                if (randomPoint < probs[i])
                {
                    switch (i)
                    {
                        case 0: // 10f 안에 랜덤포인트가 생성된경우 
                            DropTabl = Alltem.FindAll(x => x.ItemType == "Eqipment");
                            int n = Random.Range(0, 5);
                            Debug.Log(DropTabl[n].Name);
                            GetItem(DropTabl[n]);
                            break;
                        case 1: // 11f~20f안에 랜덤포인트가 생성된경우
                            DropTabl = Alltem.FindAll(x => x.ItemType == "Accessory");
                            int m = 0;
                            Debug.Log(DropTabl[m].Name);
                            GetItem(DropTabl[m]);
                            break;
                        case 2: // 21~100f안에 랜덤포인트가 생성된경우 
                            DropTabl = Alltem.FindAll(x => x.ItemType == "Potion");
                            int p = Random.Range(0, 2);
                            Debug.Log(DropTabl[p].Name);
                            GetItem(DropTabl[p]);
                            break;
                    }
                    return i;
                }
                else
                {
                    randomPoint -= probs[i]; // 랜덤포인트가 20이면 probs[0]=10f 20-10=10; 그래서 case 1:로 들어가게 된다.
                }
            }
            return probs.Length - 1;
        }
    }

장비아이템이 뜰확률은 10퍼센트

악세서리가 뜰확률은 10퍼센트

포션이 뜰확률은 80퍼센트 

 case State.DIE: // die 상태로돌입하면 
                StopAllCoroutines();
                GameManger.instance.ItemDrop();//아이템드랍
                StartCoroutine(Disapearing()); // Disapearing 코루틴 시작
                myanim.SetBool("IsRunnig", false);
                CharacterManger characterManger = GameObject.Find("Archer").GetComponent<CharacterManger>();
                characterManger.fixTarget = false;// 만약 맞았는데 현재 hp 가 0보다작거나 같다면 타겟 세팅을 위해 스타트 코루틴 켜줄 bool 조정 
                myanim.SetTrigger("Dead"); // Dead 트리거 작동으로 Dead 애니메이션 나오고

몬스터 die 상태에 2번째줄 처럼 저렇게 아이템 드랍을 추가해주면 

이런식으로 아이템이 추가가 된다.

 

IEnumerator  Drop(Item Dropitem)
    {
       
        DropPanel.SetActive(true); // 드랍 패널 활성화
        DropPanel.transform.GetChild(1).GetComponent<Image>().sprite = ItemSprite[Alltem.FindIndex(x => x.Name == Dropitem.Name)]; // 드랍패널 자식 첫번째 이미지를 인덱스화 시켜서 itemsprite에서 가져오기
        DropPanel.GetComponentInChildren<Text>().text = Dropitem.Name;// text를 이름으로 바꿔주기
        yield return new WaitForSeconds(1f);//1초뒤
        DropPanel.SetActive(false);//드랍 패널 비활성화
       

    }

코드 추가후

case 0: // 10f 안에 랜덤포인트가 생성된경우 
                            DropTabl = Alltem.FindAll(x => x.ItemType == "Eqipment");
                            int n = Random.Range(0, 4); // 장비아이템 종류 5가지 
                            StartCoroutine(Drop(DropTabl[n]));
                            Debug.Log(DropTabl[n].Name);
                            GetItem(DropTabl[n]);
                            break;
                        case 1: // 11f~20f안에 랜덤포인트가 생성된경우
                            DropTabl = Alltem.FindAll(x => x.ItemType == "Accessory");
                            int m = 0; // 악세서리 1가지
                            StartCoroutine(Drop(DropTabl[m]));
                            Debug.Log(DropTabl[m].Name);
                            GetItem(DropTabl[m]);
                            break;
                        case 2: // 21~100f안에 랜덤포인트가 생성된경우 
                            DropTabl = Alltem.FindAll(x => x.ItemType == "Potion");
                            int p = Random.Range(0, 2); // 포션 2가지 
                            StartCoroutine(Drop(DropTabl[p]));
                            Debug.Log(DropTabl[p].Name);
                            GetItem(DropTabl[p]);
                            break;

이런식으로 변경하고 

 

드랍 패널에 따로 애니메이션을 넣어주면

 

클릭을 하면 아이템이 얻어지게끔 했을때

 

 

이런식으로 흭득한 아이템이 나오게 된다.

 

이러면 이제 아이템 흭득까지는 끝났고

 

내일은 몬스터 hp바, 캐릭터 hp바 mp바  구현 예정이며 

 

시간이된다면 퀵슬롯까지 구현 할 예정이다.

반응형