Попытка получить определенное количество предметов для появления в соответствии со случайной редкостью - PullRequest
0 голосов
/ 01 февраля 2019

Итак, позвольте мне начать с того, что это моя первая попытка полностью написать систему сундуков, и я очень рад, что она действительно работает.Но я не могу понять - почему количество предметов, которые он порождает, не равно сундуку.Цикл '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
        }

    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...