dotween icon indicating copy to clipboard operation
dotween copied to clipboard

RotateAround Feature Request

Open Sven-vh opened this issue 2 years ago • 3 comments

Hi, I'm looking for a way to animate a transform that rotates around another transform. Exactly like the default Unity transform.RotateAround but in DOTween.

In 2015 you said it was on your todo list but I guess you never got around to it? Would love to have this!

Sven-vh avatar Aug 13 '23 22:08 Sven-vh

For anyone that is looking for a RotateAround I made my own (2D) extension to it. It would still be nice if DoRotateAround could be implemented though!

public static class DOTweenExtensions {
     public static Tweener DORotateAround2D(this Transform transform, Vector3 pivot, float angle, float duration) {
        Vector3 startPos = transform.position;
        float startAngle = Vector2.SignedAngle(Vector2.right, startPos - pivot);
        float endAngle = startAngle + angle;
        float startZRotation = transform.eulerAngles.z;

        return DOTween.To(() => startAngle, x => startAngle = x, endAngle, duration)
            .OnUpdate(() => {
                float radian = startAngle * Mathf.Deg2Rad;
                float radius = Vector2.Distance(pivot, startPos);

                float x = pivot.x + radius * Mathf.Cos(radian);
                float y = pivot.y + radius * Mathf.Sin(radian);

                transform.position = new Vector3(x, y, transform.position.z);
                transform.eulerAngles = new Vector3(0, 0, startZRotation + (startAngle - Vector2.SignedAngle(Vector2.right, startPos - pivot)));
            });
    }
}

Sven-vh avatar Aug 14 '23 09:08 Sven-vh

Ahoy!

I indeed wanted to add it but my solution was similar to yours, using OnUpdate, which would create problems in case someone wanted to add a custom OnUpdate because it would overwrite it. I'm going to ponder this more though (and thanks for posting this)!

Demigiant avatar Aug 14 '23 11:08 Demigiant

Why not just change the setter from x => startAngle = x to x => {startAngle = x; //OnUpdate inner code}?

FColor04 avatar Oct 06 '23 21:10 FColor04