Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Dice Master - The complete solution for your rolling needs [Unity5]

Discussion in 'Assets and Asset Store' started by Catman, Oct 9, 2015.

  1. CarterG81

    CarterG81

    Joined:
    Jul 25, 2013
    Posts:
    1,773
    It also might just be better to throw the animation and moments before it lands (or right when it does) change the texture. You could make all sides the same result. You could move the texture in the mesh so the top result is the side you want.

    Yes, it isnt something this asset does. Yes, it is cheating and gamedev trickery. But if the player never notices or doesn't care, you can get away with cheats. If the dice are thrown fast enough or they spin enough it might not be noticeable.

    Just a guess.

    A way that always works would be to make animations for dice throwing where the dice land in their side. One animation per side of the dice, and call that animation when rolling since the number is already known (loaded dice). It isnt as dynamic but it would work and wouldnt take much time to do at all, especially if you record a few good animations from this asset's dynamic rolling and for each individual side result, just change the last few key frames of the animation to land on the correct side. So six animations per dice roll for 1D6. You could also have it be a bit more dynamic, having the object move around while it animates so it looks like you're rolling anywhere but the actual spin and rotation is a premade animation.

    I am not at all experienced with physics, but I have a hard time believing you cant get this working 100% of the time with the right settings or trickery.

    Idk. Are there any magnet assets? Attaching magnets to the weighted side might make it "cling" to the right side 100% of the time, if that is a thing? Just thinking of RL physics and ways to load dice IRL that could translate over.
     
    Last edited: Feb 21, 2019
  2. jtdev

    jtdev

    Joined:
    Apr 13, 2014
    Posts:
    12
    Hi, any idea when you'll get to implement a dice string parser into your spawning api?

    Thanks!
     
  3. Catman

    Catman

    Joined:
    Jul 16, 2011
    Posts:
    47
    Hello jtdev, inside the current package, there is a test scene called "ExampleDiceSpawnFormat" that does exactly that. You can take a look at the "SpawnFormat" script, which gets strings in the "5D6" format and spawns dice based on parsing that string.
     
  4. SvenK

    SvenK

    Joined:
    Mar 17, 2016
    Posts:
    1
    FYI

    I created 3 dice with the same stats and attached the Spinner script to each. When starting the scene they all start spinning quite fast and in sync when setting the Random Axis Offset to 0.
    However, when creating a GameObject and attaching the Spawner and Spinner script to it as described in the little readme tutorial the spawned dice don’t spin as fast as the others. That happens even when using the same values as for the other 3 manually created dice and when spawning 3 dice at once they all behave the same. The spawned ones just spin slower.

    It turned out that the physics manipulation methods like AddTorque have to be called in the FixedUpdate method. So, to fix this you could modify the Spinner script similar to the following:

    Code (CSharp):
    1.      
    2. public void Trigger(Rigidbody newTargetRB = null)
    3.         {
    4.             if (newTargetRB != null)
    5.                 this.targetRB = newTargetRB;
    6.  
    7.             var actualAxis = axis;
    8.  
    9.             // Some randomization is useful to avoid always getting the same behaviour
    10.             actualAxis += new Vector3(Random.Range(-1f, 1f),
    11.                 Random.Range(-1f, 1f),
    12.                 Random.Range(-1f, 1f)) * randomAxisOffset;
    13.             actualAxis.Normalize();
    14.  
    15.             // Add the torque
    16.             //            if (targetRB) this.targetRB.AddTorque(actualAxis * spinTorque, ForceMode.Impulse);
    17.             if (targetRB)
    18.                 StartCoroutine(applySpin(targetRB, actualAxis * spinTorque));
    19.  
    20.             if (autoDestroy)
    21.                 Destroy(this);
    22.         }
    23.  
    24.         protected IEnumerator applySpin(Rigidbody rb, Vector3 spin)
    25.         {
    26.             yield return new WaitForFixedUpdate();
    27.             rb.AddTorque(spin, ForceMode.Impulse);
    28.             yield break;
    29.         }
    30.  
     
  5. Leanna23

    Leanna23

    Joined:
    Nov 5, 2020
    Posts:
    1
    I have a definite number, can i roll the dices to this definite number everytime?
     
  6. Catman

    Catman

    Joined:
    Jul 16, 2011
    Posts:
    47
    Hello, as previously stated in this thread, Dice Master uses physics to determine what number should appear and not the other way around. Unfortunately, this means that there is no sure way to make the dice report the number you want prior to throwing it.

    It would be practically impossible to achieve a realistic animation while mantaining physics constraints, hence why I added the 'weighted dice' option, which allows you to attach weights to specific faces which will force the dice to increase drastically the probability of a specific number appearing.
    You can try using the weighted D6 to see how this works (it should almost always give you a 6 if dropped from high above!).

    This won't be 100% accurate, but if you give the dice enough space to fall and roll, it should work most of the time.

    You can find more information in the FAQ on the "how can I rig the dice" question:
    http://www.michelepirovano.com/dicemaster/DM_Documentation.html

    On the other hand, if you just want to show the result, you can always rotate by code the dice so it shows the correct number, then drop it down so Dice Master registers the result when the movement stops.
     
  7. CarterG81

    CarterG81

    Joined:
    Jul 25, 2013
    Posts:
    1,773
    Another option would be to have the weighted option, but to guarantee it is 100% accurate, code an exception where if the result is different, the code moves the dice the rest of the way or causes some error animation (off the table) forcing a reroll. This way you get a guaranteed accuracy, if you need it, but still looks natural 99% of the time.

    Another option is to create your own animations for all results (predetermine the results prior), but then there isnt as much point to this asset and players wont have the more realistic physics. The former at least causes either an annoying glitch (off the table roll or some funky animation like the dice breaking) or straight up cheese (obvious change after roll), but it will only be as common as the weighted dice failing and at least you guarantee the predetermined result.
     
  8. mgrancey

    mgrancey

    Joined:
    Oct 10, 2021
    Posts:
    2
    Need to "explode" dice (rerolling and adding the numbers together), I am thinking that they best spot to do this would be on the die itself. After the shownNumber is given the face.value be before invoking the callback. Would need to add a int to store total of any rolls and setup a way to rethrow the same die. Is my thinking correct or would there be a better/more efficient way to go about this?
     
  9. Catman

    Catman

    Joined:
    Jul 16, 2011
    Posts:
    47
    Instead of modifying the die itself, you should have a separate script that controls the flow. It registers to the die's callback before throwing it, waits for the result to be rolled, detects the result, adds the value to a local sum and, if needed, re-rolls the dice.
     
  10. SimonVigMillard

    SimonVigMillard

    Joined:
    Jul 25, 2018
    Posts:
    13
    @Catman is there any easy way to make the dice automatically rotate after landing (when onShowNumber is triggered), so the value they are showing is aligned and readable for the player?
    I have dice with normal numbers (not pips, although the same question could apply with pips). So I would like the dice to rotate upon landing so the numbers on the dice are aligned with normal readable text (rather than e.g. being rotated 90 degrees because of the angle of the dice after landing). Hope the question makes sense.

    I was messing around with Quaternion.Slerp, but I'm not sure how to find the correct target rotation. E.g.:
    Code (CSharp):
    1. transform.rotation = Quaternion.Slerp(_stopRot, targetRot, Time.time - _stopTime);