Home Game Development unity – How to roll two cube so that they land on pre-determined numbers?

unity – How to roll two cube so that they land on pre-determined numbers?

0
unity – How to roll two cube so that they land on pre-determined numbers?

[ad_1]

I’m growing a recreation in Unity during which you throw two cube which ought to roll usually, however it’s important to get a predetermined output primarily based on the person enter. Say the person enters 2,5 and presses enter, the cube ought to fall on the bottom and roll in such a manner that the primary die ends in 2 and second die ends in 5

To obtain this I’ve cut up the rolling logic into two components. The first half runs till the cube contact the bottom, utilizing physics, and the second half when it begins rolling slowly I exploit leanTween.

So once I press enter the code is as proven under:

if (Input.GetKeyDown(KeyCode.Return))
{
    remodel.place = initialPosition;
    remodel.rotation = Quaternion.Euler(75f, 180f, 270f);
    initialPush = true;
    collidedWithGround = false;
    rollCompleted = false;
    // above circumstances repeated in order that in play mode when u enter the numbers once more and get the issues carried out, the values are reset           
     rigidBodyCube.useGravity = true;
     MagnitudeX = Random.Range(0f, 1000f);
     MagnitudeY = Random.Range(0f, 1000f);
     MagnitudeZ = Random.Range(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 == "Ground")
    {
        //Debug.Log("Dice collided with Ground..........");            
        initialPush = false;
        collidedWithGround = true;
    }
}


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

So to elucidate the above code, once I press enter I make initialPush true. In the mounted replace I add a sideward power on the pair of cube in order that they fall from some area above onto the bottom. The rigidBodyCube.AddTorque is in order that the cube can rotate as they hold falling.

When it hits the bottom, to ensure the ahead push is ceased, I make initialPush = false. Now I make the collidedWithGround = true. From there on lean tween takes management of stuff till it will get the outcome to a decided worth.

The worth of the needed rotations which I’ve given within the begin seems 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 works high-quality, however not in a practical manner. That is typically when it hits the bottom, the pair of cube revolve backwards, the place it truly needed to revolve ahead, which supplies an ungainly look. I attempted utilizing the rotation.lerp perform however the cube are repeatedly rotating. I attempted to go looking the best way to rotate the cube slowly utilizing physics till 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 may be this time variable which makes it arduous for me to appropriate predict when it hits the bottom accurately.

How can I get a practical answer for this downside?

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here