|
I want to roll a few dice models and be able to determine the face that is UP at the end of the roll on the various dice and use those face values in both JavaScript and/or C# scripts. First, how would you roll dice models in Unity and secondly, can someone give an example of determining which face is up on a die model in JavaScript and C#?
(comments are locked)
|
|
A quick and dirty solution would be to calculate the Vector3.Dot product of the x, y, z axises of your cube against Vector3.up. If a value is (close to) 1 that side is up, if a value is (close to) -1, that side is down (so opposite side is up). Enough info to build your own code with or do you need a script? EDIT What the heck, here's a script (the side numbers need to be adjusted to how you've got your dice set up, for me, top = 6, bottom = 1, right side = 4, left side = 3, front side = 5, back side = 2. The code is in C# but shouldn't be hard to convert to Javascript.
(comments are locked)
|
|
How you would roll dice would depend on if you're using a physics-based model, determining the result because of the dice bouncing around, or if you want to simulate the rolling to land on a specific number. If you're going for the physics model, you could do worse than to check out the source code for Rock'n'Roll dice, which I released under the Artistic License about 11 months ago. You can see a video of it here. the link is down!
Dec 16 '11 at 06:22 AM
ina
(comments are locked)
|
|
Working on a prototype which involves dice, I was having the same problem of getting the 'up' face of the rolled dice. I used the 'dot product' solution, which works pretty well. I'm now facing the extension of the problem: how to determinate the result of a die with n-faces? eg: D4 (with the resulting face at the bottom), D8, D10, etc. The configuration of each die is different, and I wonder if another approach would work. Some hints maybe:
Any suggestions?
(comments are locked)
|
|
Note that in the above code there is dead zone of 0.99. However unlikely if those values come up the dice value will be 0, which is likely not what you want, change some of the operators to be <= or >= etc..
(comments are locked)
|
