Search Unity

Unity 3d how to Roll a dice to get a predetermined result

Discussion in 'Scripting' started by enajhernandez01, Mar 3, 2018.

  1. enajhernandez01

    enajhernandez01

    Joined:
    Nov 6, 2017
    Posts:
    7
    I want to roll a dice with a predetermined value, for example i want a 5 value of dice i have a screenshot of code which rotates a random number. hope you can help me Thanks.



    i also tried the comment and uncommented code but it always shows a random result. hope you really can help me.
     
  2. enajhernandez01

    enajhernandez01

    Joined:
    Nov 6, 2017
    Posts:
    7
  3. enajhernandez01

    enajhernandez01

    Joined:
    Nov 6, 2017
    Posts:
    7
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    Do not bump threads more in less than 24 hours.
     
  5. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,794
    I think the answer is not really trivial, although I'm not that experienced with physics.

    The easiest way I can think of from the top of my head, is to use pre-made animations (like a few of them), that you know how they end up but you pre-rotate the die (like you animate an empty parent gameobject, but you have the die in a child, so you can rotate it).
     
  6. Either make separate premade animations where the end result consists with the result you made with the random. Or create one animation and multiple setups with materials (the expected side on the top of the object).

    You do not want to do it through physics because the physx API is not deterministic.
    http://www.nvidia.com/object/physx_knowledge_base.html
     
  7. DmitryMatveev

    DmitryMatveev

    Joined:
    May 9, 2013
    Posts:
    4
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Since Dmitry necroed us back to life, and since Lurk posted the following in 2018:

    I think back then the physics engine didn't support running it yourself, but now it does.

    https://docs.unity3d.com/ScriptReference/Physics-autoSimulation.html
    https://docs.unity3d.com/ScriptReference/Physics.Simulate.html

    And thus, non-deterministic is fine if you can run the simulation yourself.

    So... all in one frame: turn off auto simulation, blast a dice randomly into your playfield, running the physics yourself and observing the position and rotation of the simulated Rigidbody as it goes into the scene, bounces and tumbles a bit, etc., and finally lands with a face up.

    Now Destroy() the Rigidbody, adjust the dice faces to show the one you want up and then in real time play back the transient stored simulation of the rolling dice, which will be deterministic because you're just driving it with stored rotations and positions.

    Heck you could roll any number of dice simultaneously and do the above once all have become quiescent.

    Bust out your Cinemachine and some clever follows and each roll could be totally cinematic... if you know a bad number is coming, start playing dread-worthy music right before the dice comes to a rest.
     
    Bunny83 likes this.