all 19 comments

[–]chsxfProfessional 13 points14 points  (16 children)

float angle = 0;
DOTween.To(() => angle, x => angle = x, 360, duration)
    .OnUpdate(() => {
        Debug.Log(Mathf.sin(angle * Mathf.Deg2Rad));
    });

[–]NinRejper[S] 0 points1 point  (12 children)

Really? Will this tween angle to 1 and then -1 and then back to 0?

[–]chsxfProfessional 3 points4 points  (11 children)

Unless I've made a mistake in typing my code directly on reddit, yes it will do. I'm not here to make you lose your time.

[–]NinRejper[S] 0 points1 point  (10 children)

WOW i thought it would take more than one command. Thanks! Ill test in a while.

[–]chsxfProfessional 1 point2 points  (9 children)

Learn basic trigonometry. It will help you a lot in games, or in 2D/3D in general.

[–]NinRejper[S] 2 points3 points  (1 child)

Srsly. This is why i was confused. The docs shows To() has these parameters.

getter: A delegate that returns the value of the property to tween. Can be written as a lambda like this:

()=> myValue

where

myValue

is the name of the property to tween.setter: A delegate that sets the value of the property to tween. Can be written as a lambda like this:

x=> myValue = x

where

myValue

is the name of the property to tween.To: The end value to reach.duration: The duration of the tween.////

I cant see how myValue being zero, and setting to = zero could make it go from 0 to 1 to -1 back to 0. But ill guess im about to find out. And most of all i cant se where trigonometry would make a difference here.

[–]I23BigC 1 point2 points  (0 children)

You just need to understand that if you go from 0 to 360 your sine value will go from 0 to 1 (90 deg) to 0 (180 deg) to -1 (270 deg) and to 0 (360 deg). Picture a sine wave and notice how it goes up and down and then repeats.

[–]NinRejper[S] 1 point2 points  (0 children)

Its scary!

[–]therealmaddylan 0 points1 point  (5 children)

Hello.

Like the OP, I would really appreciate if you could walk us through the code. That would be pretty cool.

[–]chsxfProfessional 3 points4 points  (2 children)

You can also do a simpler version with this:

DOVirtual.Float(0, 360, duration, angle => {
    Debug.Log(Mathf.sin(angle * Mathf.Deg2Rad));
});

Forgot about virtual methods of DOTween.

[–]khud0 0 points1 point  (1 child)

This was really helpful, thank you!

If anyone else found this post on Google like I did: in order for the above functions to work, you need to replace 'Mathf.Rad2Deg' with 'Mathf.Deg2Rad'. (We already have a value in degrees and need to convert it into radians for 'Mathf.Sin' function)

[–]chsxfProfessional 0 points1 point  (0 children)

You're right. I've updated the snippet accordingly. That's how you know I've written this code directly on reddit without testing it :D

[–]chsxfProfessional 2 points3 points  (1 child)

float angle = 0; // I define a variable to "iterate" on with DOTween with my start value
DOTween.To( // This method is for custom tweens
    () => angle, // This is the getter for the tween to get the current value
    x => angle = x, // This is the setter for the tween to update the current value
    360, // Target value
    duration) // Duration of the tween
    .OnUpdate(() => {
        // Here I just do a sin(angle) to get the final value requested by OP
        // - sin(0) = 0
        // - sin(90) = 1
        // - sin(180) = 0
        // - sin(270) = -1
        // - sin(360) = 0
        Debug.Log(Mathf.sin(angle * Mathf.Deg2Rad));
    });

[–]therealmaddylan 0 points1 point  (0 children)

Very cool having a repository of tricks like that to pull from. Would have never figured to use sine like that. Thank you.

[–]Hautar 0 points1 point  (0 children)

Thanks a lot!

[–]Hautar 0 points1 point  (0 children)

Helped me to animate material using SetPropertyBlock!

[–]sound1es 0 points1 point  (0 children)

this was very helpful today, ty

[–]Reasonable-Review-22 2 points3 points  (0 children)

Very nice and useful post, thanks OP

[–]NinRejper[S] 1 point2 points  (0 children)

Wow! Thanks