0
\$\begingroup\$

I'm trying to create script that rotates object in given axis by set amount of degrees.

That's my 2 core methods

public Vector3 rotation;

public void RotateDegreesFast ()
{
    transform.DOLocalRotate (GetTargetRotation (), rotationTime);
}

private Vector3 GetTargetRotation ()
{
    Vector3 newRotation = new Vector3 (
        rotation.x + transform.localEulerAngles.x,
        rotation.y + transform.localEulerAngles.y,
        rotation.z + transform.localEulerAngles.z)

    return newRotation;
}

I was using this script for a little while, it was working perfectly in Y and Z axis. Now I try to rotate object 45 degrees in X axis (rotation = new Vector3(45f, 0f, 0f)). It works until rotation in x value equals 135. Transform.localEulerAngles.x is returning 45 instead of 135. So it's stuck between 90 and 135 degrees. I was trying to use transform.localRotation.eulerAngles.x instead, but it doesn't work as well.

I am aware this works that way because specific Euler angle can be represented in many ways. I also know that DOTween has DOLocalRotateQuaternion method, but the thing is that this component is often reused by a designer that sets value of rotation variable in the inspector. It's way easier to use Euler angles for him.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Have you considered simply converting the designer's Euler angles to a quaternion, before passing them to DOTween's quaternion method? Also, please note that you can edit an existing question, instead of deleting it and re-positng? \$\endgroup\$
    – DMGregory
    Commented Sep 8, 2020 at 15:01
  • \$\begingroup\$ Thanks, somehow I didn't think about it. \$\endgroup\$
    – Solipsis
    Commented Sep 8, 2020 at 15:37

1 Answer 1

2
\$\begingroup\$

Final code is easier than the one I posted. I just didn't think about doing it this way.

public Vector3 rotation;

public void RotateDegreesFast () 
{
    Quaternion newRotation = transform.localRotation * Quaternion.Euler(rotation);
    transform.DOLocalRotateQuaternion(newRotation, rotationTime); 
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .