Unity3D Adjusting CUBE "endRotation" on right ( x or z) axis

I am new in Unity3D scripting especially when it comes to specifics of Transform, Quaternions, Vector3. Down there is a script. When I roll the cube it flips in the right direction. However when cubes' local axis doesn't match the world axis the cube's endRotation is performed on the wrong axis. Can anybody help me to resolve this issue : "endRotation" would flip the cube on right axis regardless of the relation between LOCAL axis <=> WorldAxis. I was trying to resolve it for a week. Of cause no success. Further down there code and video.

      if (Input.GetAxis("Horizontal") > buttonDown)

. . .

    public IEnumerator FlipTheCube(Vector3 direction)
    startFliping = false;

    float rollStartTime = 0;
    float rollAngle = 90;
    float halfWidth = transform.localScale.z / 2;
    Vector3 pointAround = transform.position + (Vector3.down * halfWidth) + (direction * halfWidth);
    Vector3 rollAxis = Vector3.Cross(Vector3.up, direction);

    Quaternion rotation = transform.rotation;
    Quaternion **endRotation** = rotation * Quaternion.Euler(rollAxis * rollAngle);
    Vector3 endPlacement = transform.position + direction;

    float oldAngle = 0;

    while (rollStartTime < rollDurtnTime)
        yield return new WaitForEndOfFrame();
        rollStartTime += Time.deltaTime;
        float newAngle = (rollStartTime / rollDurtnTime) * rollAngle;
        float rotateThrough = newAngle - oldAngle;
        oldAngle = newAngle;

        transform.RotateAround(pointAround, rollAxis, rotateThrough);

    transform.position = endPlacement;
    transform.rotation = **endRotation**;

    startFliping = true;

Here is a link to the youtube video First part of video If cubes local axis is matching world axis the "endRotation" is flipping correctly Second part of video as soon cubes local axis is not matching world axis "endRotation" goes wrong.

Read more here: https://stackoverflow.com/questions/64892738/unity3d-adjusting-cube-endrotation-on-right-x-or-z-axis

Content Attribution

This content was originally published by RSRY at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: