尝试根据随机的稀有度获得一定数量的物品

时间:2019-02-01 01:40:50

标签: c# unity3d

因此,我首先要说这是我第一次尝试完全自己编写一个胸部系统,我很高兴它确实有效。但是我不知道-为什么它产生的项目数量不等于ChestLevelInt。 “ for”循环应一直运行,直到达到ChestLevelInt。但是相反,我只是从对象数组中产生随机数量的项目。没有明显的押韵或理由。

我相信我的“ for”循环已陷入混乱,但我只是无法弄清发生了什么,而我正试图弄清楚这一点,我正迷失了它。

我真的很感谢,因为我在编码方面花了很多时间,并将自己研究这个问题。

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

public class chestScript : MonoBehaviour {

    public GameObject[] itemArray; //array of items in chest


    private float chestLevel; //what rarity this chest is.. randomly assigned
    private int chestLevelInt;

    private float chestItemNumber; //what array item # is being called to instatiate from chest
    private int chestItemNumberInt;

    private Rigidbody playerRigidbody;
    private bool chestOpenTF; // has the chest been opened
    private string[] chestColor;

    Material chestMaterial;

    public GameObject itemSpawnPoint;

    // Use this for initialization
    void Start () {
        GetComponent<Rigidbody>();
        chestMaterial = GetComponent<Renderer>().material;

        chestOpenTF = false;

        chestLevel = Random.Range(0.0f, 5.0f);  //give chestLevel a random value between 1 & 6
        chestLevelInt = (int)chestLevel;



        chestRarity(); //calls the class that sets the chest color


    }

    // Update is called once per frame
    void Update () {
        chestItemNumber = Random.Range(0.0f, 4.0f);
        chestItemNumberInt = (int)chestItemNumber;
    }

    private void OnCollisionStay(Collision collision)
    {

        if (chestOpenTF == false && collision.collider.name == "Player" && Input.GetKey(KeyCode.E)) { //allows player to open using Key E if in proximity
            print("This chest has been opened T/f: " + chestOpenTF);
            openChest();
        }
        if (chestOpenTF == true && collision.collider.name == "Player" && Input.GetKey(KeyCode.E)) { //won't let a player open a chest twice
            print("This chest has been opened T/f:" + chestOpenTF);
        }
    }

    void openChest() {

        for (int i = 0; i <= chestLevelInt; i++) {



           //////this whole if segment is a rough way to make it change the Itemnumber to spawn but stay withing the bounds of 1-4
            if (chestItemNumberInt == 4) { 
                chestItemNumberInt -= 1;
            }
            if (chestItemNumberInt >= -1)
            {
                chestItemNumberInt += 1;
            }
            ///////////////////////////


            Instantiate(itemArray[chestItemNumberInt], itemSpawnPoint.transform); //instatiates random items from the chest out of the items array

            print(chestItemNumberInt);
            chestOpenTF = true; //so you cant open the chest again
            chestMaterial.color = Color.grey; //show that the chest has been opened
        }

    }

    void chestRarity() {

        print(chestLevelInt); //for testing purposes to see what rarity has been chosen

        if (chestLevelInt == 1) {
            chestMaterial.color = Color.green; //weakest
        }
        if (chestLevelInt == 2)
        {
            chestMaterial.color = Color.blue; //second weakest
        }
        if (chestLevelInt == 3)
        {
            chestMaterial.color = Color.magenta; //second strongest
        }
        if (chestLevelInt == 4)
        {
            chestMaterial.color = Color.yellow; //the strongest
        }

    }
}

0 个答案:

没有答案