Demigiant / dotween

A Unity C# animation engine. HOTween v2
http://dotween.demigiant.com
Other
2.29k stars 344 forks source link

RotateAround Feature Request #649

Open Sven-vh opened 11 months ago

Sven-vh commented 11 months ago

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 commented 11 months ago

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)));
            });
    }
}
Demigiant commented 11 months ago

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)!

FColor04 commented 9 months ago

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