unity – scale and rotate a dice computerized the dice isn’t scaling and rotating to the top. learn how to repair?

unity – scale and rotate a dice computerized the dice isn’t scaling and rotating to the top. learn how to repair?

[ad_1]

the scaling and rotating each are working.

if the automated flag bool is fake and that i press the G key every time i can see that the dice is scaling and rotating to the top for instance if it is rotating down then the dice scale to the minSize 0.1,0.1,0.1 and rotation 0,180 or -180,0 identical for maxSize.

the issue is when the automated flag. bool variable is ready to true.
then the dice is cutting down/up nonstop and that i can see that the dice whether or not cutting down or up isn’t end the scaling and rotating to every course. close to the top it is beginning scaling and rotating again to the opposite course.

why when it is robotically scaling and rotating it isn’t attain precisely to the top every cycle?

utilizing System.Collections;
utilizing Unity.VisualScripting;
utilizing UnityEngine;
 
public class Scaling : MonoBehaviour
{
    public GameObject objectToScale;
    public float length = 1f;
    public Vector3 minSize;
    public Vector3 maxSize;
    public bool scaleUp = false;
    public Coroutine scaleCoroutine;
 
    public bool computerized = false;
    public bool coroutineIsRunning = false;
 
    non-public void Begin()
    {
 
    }
 
    non-public void Replace()
    {
        if (computerized)
        {
            if (!coroutineIsRunning)
            {
                Scale();
            }
        }
        else
        {
            if (Enter.GetKeyDown(KeyCode.G))
            {
                Scale();
            }
        }
    }
 
    non-public void Scale()
    {
        scaleUp = !scaleUp;
 
        if (scaleCoroutine != null)
        {
            StopCoroutine(scaleCoroutine);
        }
 
        scaleCoroutine = StartCoroutine(ScaleOverTime(objectToScale, scaleUp ? maxSize : minSize, length, scaleUp));
    }
 
    int c = 0;
    non-public IEnumerator ScaleOverTime(GameObject targetObj, Vector3 toScale, float length, bool isScalingUp)
    {
        float counter = 0;
        Vector3 startScaleSize = targetObj.remodel.localScale;
        Quaternion startRotation = targetObj.remodel.rotation;
 
        // Generate a random angle (0 or 1)
        int randomAngle = Random.Vary(0, 2);
        // Set the rotation angle primarily based on the random worth
        float t = (randomAngle == 0) ? 180f : -180f;
 
        // Select the top rotation primarily based on the scaling course and apply randomization for each up and down scaling
        Quaternion endRotation = isScalingUp ? Quaternion.Euler(0, 0, 0) : Quaternion.Euler(0, t, 0);
 
        coroutineIsRunning = true;
 
        whereas (counter <= length)
        {
            counter += Time.deltaTime;
            float lerpFactor = Mathf.Clamp01(counter / length); // Guarantee lerp issue is clamped.
 
            targetObj.remodel.localScale = Vector3.Lerp(startScaleSize, toScale, lerpFactor);
            targetObj.remodel.rotation = Quaternion.Lerp(startRotation, endRotation, lerpFactor);
 
            // Simply earlier than the loop finishes, guarantee we attain the precise last values
            if (counter >= length - Time.deltaTime)
            {
                targetObj.remodel.localScale = toScale;
                targetObj.remodel.rotation = endRotation;
            }
 
            yield return null;
        }
 
        coroutineIsRunning = false;
 
        // Sign finish of animation for pause, and so on.
        c++;
        Debug.Log("Completed! " + c);
        if (c == 2)
        {
            //Time.timeScale = 0; // Think about using a pause technique that does not have an effect on Time.timeScale for extra advanced video games
        }
    }
}

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply