Friday, October 28, 2022
HomeGame Developmentc# - Methods to get predermined Output of Two Dices rolled randomly...

c# – Methods to get predermined Output of Two Dices rolled randomly in Unity in a sensible method


I’m growing a sport in Unity through which when you throw two dices it ought to roll usually, however you must get a predetermined output based mostly on the consumer enter. Say consumer enter 2,5 and presses enter, the cube ought to fall on the bottom and roll in such a method that the primary cube leads to 2 and second cube leads to 5

To realize this I’ve break up the rolling logic into two elements. First half is until the cube touches the bottom I exploit the physics and the second half when it begins rolling slowly I exploit leanTween

So after I press enter the code is as proven beneath:

`  if (Enter.GetKeyDown(KeyCode.Return))
        {
            rework.place = initialPosition;
            rework.rotation = Quaternion.Euler(75f, 180f, 270f);
            initialPush = true;
            collidedWithGround = false;
            rollCompleted = false;
            // above situations repeated in order that in play mode when u enter the numbers once more and get the issues performed, the values are reset           
             rigidBodyCube.useGravity = true;
             MagnitudeX = Random.Vary(0f, 1000f);
             MagnitudeY = Random.Vary(0f, 1000f);
             MagnitudeZ = Random.Vary(0f, 1000f);
        }`
 `personal void FixedUpdate()
    {
       if(initialPush)
        {
            rigidBodyCube.AddForce(new Vector3(pushForce, -pushForce,0f));                      
            rigidBodyCube.AddTorque(MagnitudeX, MagnitudeY, MagnitudeZ);           
        }
        if (collidedWithGround && !rollCompleted)
        {
            RollDiceToDesiredNumber();
        }


    }`


personal void OnCollisionEnter(Collision different)
{
if (different.gameObject.title == “Floor”)
{
//Debug.Log(“Cube collided with Floor……….”);
initialPush = false;
collidedWithGround = true;

    }
 }
`
`personal void RollDiceToDesiredNumber()
    {
        int diceNumber = int.Parse(finalResult.GetComponent<TMP_InputField>().textual content);
        LeanTween.rotateLocal(gameObject, wantedRotation[diceNumber - 1], timeRoll).setOnComplete(() => rollCompleted = true);
    }`

So to clarify the above code,after I press enter I make initialPush true. Within the fastened replace I add a sideward pressure on the pair of cube in order that it falls from some house above onto the bottom.The rigidBodyCube.AddTorque is in order that the dices can rotate because it retains falling

When it hits the bottom, to verify the ahead push is ceased, I make preliminary push = false. Now I make the collidedWithGround =true. From there on lean tween takes management of stuff until it will get the end result to a decided worth.

The worth of the needed rotations which I’ve given within the begin. It appears to be like like this:

wantedRotation[0] = new Vector3(270f, 0, 0); wantedRotation[1] = new Vector3(0, 0, 0); wantedRotation[2] = new Vector3(0, 0, 270f); wantedRotation[3] = new Vector3(0, 0, 90f); wantedRotation[4] = new Vector3(180f, 0f, 0f); wantedRotation[5] = new Vector3(90f, 0f, 0f);

It really works fantastic, however not in a sensible method. That’s generally when it hits the bottom, the pair of dices revolve backwards, the place it really needed to revolve ahead, which supplies an ungainly look.I attempted utilizing the rotation.lerp operate however the dices are continously rotating.I attempted to go looking tips on how to rotate the cube slowly utilizing physics until the specified rotation however I’m not getting any correct outcomes. I attempted to make use of Lean tween from the second it was thrown until it hits the bottom, however there’s this time variable which makes it exhausting for me to appropriate predict when it hits the bottom appropriately.

So tips on how to get a sensible answer for this downside?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments