Sunday, July 17, 2022
HomeGame Developmentunity - Rotating Dice by 90 levels causes

unity – Rotating Dice by 90 levels causes


I am making a sport the place I might just like the participant to have the ability to rotate a dice by 90 levels on the x or z axis by urgent a button. I’ve gotten pretty far alongside within the course of, however I’ve run into two issues right here. #1. When utilizing the next code, the x-rotation will generally get caught toggling between 90 and -180 levels when scrolling ahead. #2. I might like to have the ability to rotate the dice at all times to the gamers perspective (for instance when the participant proper clicks the dice will at all times rotate 90 levels to their proper, as an alternative of wherever the z-axis has been repositioned by the opposite rotation). Any assist with both of those points could be drastically appreciated.

personal void Replace()
    {
        if(Enter.mouseScrollDelta.y > 0 && !isRotating)
        {
            StartCoroutine(Roll90(new Vector3(90, 0, 0)));
        }
        if (Enter.mouseScrollDelta.y < 0 && !isRotating)
        {
            StartCoroutine(Roll90(new Vector3(-90, 0, 0)));
        }
        if(Enter.GetMouseButtonDown(0) && !isRotating)
        {
            StartCoroutine(Roll90(new Vector3(0, 0, 90)));
        }
        if (Enter.GetMouseButtonDown(1) && !isRotating)
        {
            StartCoroutine(Roll90(new Vector3(0, 0, -90)));
        }
    }
    
    IEnumerator Roll90(Vector3 rot)
    {
        currentRotation = remodel.eulerAngles;
        targetRotation = currentRotation + rot;
        isRotating = true;

        if(rot.x != 0)
        {
            whereas (currentRotation.x != targetRotation.x)
            {
                currentRotation.x = Mathf.MoveTowardsAngle(currentRotation.x, targetRotation.x, rotationTime * Time.deltaTime * 100);
                remodel.eulerAngles = currentRotation;
                yield return null;
            }

            if(currentRotation.x != targetRotation.x)
            {
                remodel.eulerAngles = targetRotation;
            }
        }

        if (rot.z != 0)
        {
            whereas (currentRotation.z != targetRotation.z)
            {
                currentRotation.z = Mathf.MoveTowardsAngle(currentRotation.z, targetRotation.z, rotationTime * Time.deltaTime * 100);
                remodel.eulerAngles = currentRotation;
                yield return null;
            }

            if (currentRotation.z != targetRotation.z)
            {
                remodel.eulerAngles = targetRotation;
            }
        }

        isRotating = false;
    }
```

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments