Drop item based on die roll
How could I setup an array that would take the value of the die roll and drop an item that was setup for that value? ie: roll 1 - sword, roll 2 - axe roll 3 - knife ect... What I'm trying to do is at the end of each level I have the boss setup to bring the die screen up when the boss is killed, then the player clicks the die and whatever the die lands on the player will get that item. Here is what I have for the die script. Right now the player kills the boss and the screen pops up and I can click and roll the die.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class DiceRoll : MonoBehaviour
{
public Sprite[] Dice;
private bool isRolling;
private float totalTime;
private float intervalTime;
private int currrentDie;
private bool dieRolled;
private int currentTotal;
Image die;
void Start()
{
die = GameObject.Find("DieImage").GetComponent<Image>();
Init();
}
private void Init()
{
totalTime = 0.0f;
intervalTime = 0.0f;
currrentDie = 0;
dieRolled = false;
die.sprite = Dice[currrentDie];
}
void Update()
{
if (isRolling)
{
intervalTime += Time.deltaTime;
totalTime += Time.deltaTime;
if (intervalTime >= 0.1f)
{
//change die & rotation
currrentDie = Random.Range(0, 6);
die.transform.Rotate(0, 0, Random.Range(0, 360));
//set image to selected die
die.sprite = Dice[currrentDie];
intervalTime -= 0.1f;
}
if (totalTime >= 2.00f)
{
isRolling = false;
dieRolled = true;
}
}
}
public void DieImage_Click()
{
if (!dieRolled)
{
isRolling = true;
}
if (!isRolling)
{
Init();
isRolling = true;
}
}
public void PanelTestButton_Click()
{
GameObject panel = GameObject.Find("DiePanel");
Animator animator = panel.GetComponent<Animator>();
animator.Play("DiePanelOpen");
}
public void PanelOKButton_Click()
{
GameObject panel = GameObject.Find("DiePanel");
Animator animator = panel.GetComponent<Animator>();
animator.Play("DiePanelClose");
}
}
Are you wanting to drop the item within the Game World, or place said Item directly into an inventory? I'm assu$$anonymous$$g the Items are already set up, so there would be an if statement for each currentDie integer value placing that item either in the game world or the inventory itself depending on what you are wanting.
Yes there is an inventory already in place. The item would be placed into the inventory depending on what your roll is. I would like to be able to set the 6 items manually in a array or something and add it to inventory. I know how to add items to the inventory and how to have items drop. $$anonymous$$y problem is I'm not sure how to set up the die to know what item to add. How do i match the value of the die with the item in the array?
Answer by oStaiko · Nov 22, 2016 at 04:38 PM
What you want is a switch function, but then again I may just be saying that as I am a switch enthusiast.
Weapon[] Swords;
Weapon[] Bows;
Weapon[] Armor;
void GetItem (int result)
{
switch (result)
{
case 0: return Swords[Random.Range(0, Swords.Length);
case 1: return Bows[Random.Range(0, Bows.Length);
case 0: return Armor[Random.Range(0, Armor.Length);
}
}
I mean, you could ALSO do an array of arrays... but that's no fun...
Weapon[][] weaponList = new Weapon[][]
{
new Weapon[] {sword1, sword2, sword3},
new Weapon[] {bow1, bow2, bow3, bow4},
new Weapon[] {armor1, armor2, amor3}
};
Weapon GetItem (result)
{
return weaponList[result][Random.Range(0,weaponList[result].Length);
}
Thank you, I was able to use a switch to get it working.