Friday, October 28, 2022
HomeGame Developmentunity - The best way to roll two cube in order that...

unity – The best way to roll two cube in order that they land on pre-determined numbers?


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

To realize this I’ve break up the rolling logic into two elements. The primary half runs till the cube contact the bottom, utilizing physics, and the second half when it begins rolling slowly I take advantage of leanTween.

So once 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 circumstances repeated in order that in play mode when u enter the numbers once more and get the issues finished, the values are reset           
     rigidBodyCube.useGravity = true;
     MagnitudeX = Random.Vary(0f, 1000f);
     MagnitudeY = Random.Vary(0f, 1000f);
     MagnitudeZ = Random.Vary(0f, 1000f);
}
non-public void FixedUpdate()
{
   if(initialPush)
    {
        rigidBodyCube.AddForce(new Vector3(pushForce, -pushForce,0f));                      
        rigidBodyCube.AddTorque(MagnitudeX, MagnitudeY, MagnitudeZ);           
    }
    if (collidedWithGround && !rollCompleted)
    {
        RollDiceToDesiredNumber();
    }
}
non-public void OnCollisionEnter(Collision different)
{
    if (different.gameObject.title == "Floor")
    {
        //Debug.Log("Cube collided with Floor..........");            
        initialPush = false;
        collidedWithGround = true;
    }
}


non-public 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, once I press enter I make initialPush true. Within 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 preserve falling.

When it hits the bottom, to verify 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 end result to a decided worth.

The worth of the wished rotations which I’ve given within the begin appears 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 practical means. That’s generally when it hits the bottom, the pair of cube revolve backwards, the place it truly needed to revolve ahead, which supplies a clumsy look. I attempted utilizing the rotation.lerp operate however the cube are repeatedly rotating. I attempted to look how you can 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 onerous for me to appropriate predict when it hits the bottom accurately.

How can I get a practical resolution 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