Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Jan 26, 2018 at 10:23 AM by meat5000 for the following reason:

The question is answered.... and please do more research and trial before writing more questions.

avatar image
0
Question by Reedex · Jan 18, 2018 at 09:26 AM · rolldice

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.

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bonfire-Boy · Jan 18, 2018 at 09:49 AM 1
Share

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.

avatar image Reedex Bonfire-Boy · Jan 18, 2018 at 09:51 AM 0
Share

a formula to write in weapon script,for its damage,.

avatar image dev-waqas Reedex · Jan 18, 2018 at 09:55 AM 0
Share

Bonfire-Boy is right your formula will produce $$anonymous$$imum 3 and maximum 9 because you are adding one at the end.

Show more comments
avatar image andreim44 · Jan 18, 2018 at 10:10 AM 1
Share

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.

avatar image Harinezumi andreim44 · Jan 18, 2018 at 10:16 AM 0
Share

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.

avatar image Reedex andreim44 · Jan 18, 2018 at 10:20 AM 0
Share

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.

avatar image Reedex Reedex · Jan 18, 2018 at 10:27 AM 0
Share

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();
                     }

1 Reply

  • Sort: 
avatar image
2
Best Answer

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);

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Reedex · Jan 18, 2018 at 10:22 AM 0
Share

not sure how to implement.what am i suppose to do with this?put on sword,..? how do i "extract" variable

avatar image Bonfire-Boy Reedex · Jan 18, 2018 at 10:41 AM 1
Share

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.

avatar image Harinezumi Bonfire-Boy · Jan 18, 2018 at 10:45 AM 0
Share

Exactly: int Attack = nDxPlusC(2, 4, 1); // for 2d4+1 damage By the way, that's not an attack, that's potentialDamage ;)

Show more comments

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges