The question is answered.... and please do more research and trial before writing more questions.
what is the formula for "dice roll" damage (for example : 2d4 +1)
meaning,two foursided dice, so damage can be lowest 2 biggest 8, and between.
What are you looking for? A way of generating an individual roll, a formula for expected damage, or a probability distribution?
And are you sure about the 2-8 thing? I'd have thought that "2d4+1" would give you a number in the range 3-9.
a formula to write in weapon script,for its damage,.
Bonfire-Boy is right your formula will produce $$anonymous$$imum 3 and maximum 9 because you are adding one at the end.
I think we need a little more context here, I'm not sure what exactly you're looking for. In my D&D app, I have a Dice class which outputs a Random value based on the dice type, and then just add more of these Dice for a roll with more dice.
You should NOT try a Random(2, 8), based on "that's the range" since the chances aren't equal when rolling 2 dice to roll a 2 as they are to roll a 5, so that's something to look out for. But for a proper answer, please provide more details.
Exactly, sum of dices generates a geometric distribution, not a uniform one, e.g. the more dice you have, the closer you will get to the expected value of the sum (e.g. the most likely value from 2d6 is 7). Actually, there is an easy formula for the expected value of the sum of n dices: n x (max + $$anonymous$$) / 2.
so basically,i'd like this to be the weapons script,with XdX variables,. and when i attack, my attack key pressed would extract the sum rolled.and use it to damage. for now this is the attack raycast script.
i'd like to change the random 15-18 to dice roll
raycastedObj = hit.collider.gameObject;
enemyxp xpEnemyS = raycastedObj.GetComponent<enemyxp> ();
int Attack = Random.Range (15, 18);
if (xpEnemyS.enDEF <= Attack)
{
xpEnemyS.currHealthEnemy -= Attack - xpEnemyS.enDEF;
Debug.Log (Attack - xpEnemyS.enDEF);
DamageTextík.text = (Attack - xpEnemyS.enDEF).ToString();
}
else
{
xpEnemyS.currHealthEnemy -= 1;
DamageTextík.text = (1).ToString();
}
Answer by Harinezumi · Jan 18, 2018 at 10:10 AM
Random.Range(minValue, maxValue)
is your friend here (but watch out, the version with int
parameters is inclusive-exclusive, because it was designed for getting random values from indexable collections like arrays).
To make it easier, here is a generic solution:
public static int Dx (int numSides) { return Random.Range(1, numSides + 1); }
public static int nDx (int n, int x) {
int result = 0;
for (int i = 0; i < n; ++i) { result += Dx(x); }
return result;
}
public static int nDxPlusC (int n, int x, int c) { return nDx(n, x) + C; }
In your case you would use the above the following way: int damage = nDxPlusC(2, 4, 1);
not sure how to implement.what am i suppose to do with this?put on sword,..? how do i "extract" variable
You just replace your int Attack = Random.Range(15,18)
in the code you posted above, with the function call that Harinezumi has given you.
Exactly: int Attack = nDxPlusC(2, 4, 1); // for 2d4+1 damage
By the way, that's not an attack, that's potentialDamage
;)
Follow this Question
Related Questions
How to add force to dice so it rolls all over the table and it turns in while being thrown 1 Answer
Billboard shader lock roll(z) rotation 0 Answers
Anyone know how to rotate an object using yaw, pitch, roll of the HTC VIVE and SteamVR? 0 Answers
Dice going through walls using Raycast hit distance 0 Answers
Roll cuboid does not work properly 1 Answer