1
\$\begingroup\$

I have a function in my Unity game which marks a given cell GameObject as a valid cell for next move by changing the texture on it. I wanted to spice things up a bit by adding some tweens to the changing cells so I decided to use a simple tween in DOTween - I wanted to rotate the cell 360 degrees in-place while changing the texture when it's facing away from the player (so that the texture doesn't suddenly pop to a different one when it's visible). So I did something like that:

public void markAsLegal()
{
    centerPivot.DOLocalRotate(new Vector3(180, 0, 0), .5f, RotateMode.WorldAxisAdd);
    this.GetComponent<Renderer>().material.mainTexture = Resources.Load("textures/legal_hex") as Texture2D;
    centerPivot.DOLocalRotate(new Vector3(180, 0, 0), .5f, RotateMode.WorldAxisAdd);
    isLegal = true;
}

This, however, doesn't seem to wait for one rotate before starting the next one and so the cells end up in some weird middle ground, slanted. How can I make DOTween/Unity know that I want these commands to be executed in progression, ie. 180 degree rotation ends -> change texture -> finish the remaining 180 degrees?

\$\endgroup\$
1
\$\begingroup\$

Thats because you are not waiting for the first tween to complete.

first you have to wait for fist tween to complete then on completion of first tween you have change texture and start second tween and by the end of the second tween make "isLegal = true;" so to achieve this you have to do this:

private void markAsLegal()
    {
        centerPivot.DOLocalRotate(new Vector3(180, 0, 0), .5f, RotateMode.WorldAxisAdd).OnComplete(ChangeTexture);
    }

    private void ChangeTexture()
    {
        this.GetComponent<Renderer>().material.mainTexture = Resources.Load("textures/legal_hex") as Texture2D;
        centerPivot.DOLocalRotate(new Vector3(360, 0, 0), .5f, RotateMode.WorldAxisAdd).OnComplete(EndRotation);
    }

    private void EndRotation()
    {
        isLegal = true;
    } 

So what i did here First i started a tween to do the first half of the rotation and on the completion of the first tween i call "ChangeTexture()" to change the textures and start second to tween to complete the rotation and when second tween completed i call the "EndRotation()" to do this "isLegal = true;".

I hope this will help.

\$\endgroup\$
3
  • \$\begingroup\$ Thank you, this looks like exactly the kind of thing I'd like to do! Is there, however, a cleaner method of achieving this, ie. without cluttering your code with each and every step of the process as a separate method? Here it may not be as bad but if I wanted to do 5 or 7 steps in quick succession, each 0.1s long, this would yield 6 unnecessary methods, each of them being basically a one-liner DOTween command. \$\endgroup\$ – Straightfw Aug 30 '19 at 17:39
  • 1
    \$\begingroup\$ Yes there is, you could use lambda function to reduce the number of the function if you want to do that then this is the way. .OnComplete(()=>{code you want to run on complete.}). if you need more help for lambda function let me know... \$\endgroup\$ – Swati Aug 31 '19 at 8:31
  • \$\begingroup\$ great, that does the trick, thanks a lot! :) \$\endgroup\$ – Straightfw Sep 1 '19 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.