Making a function that executes once change things gradually

public void Charge()
if(transform.rotation.eulerAngles.x < 55f)
translate = new Vector3(0, 0.01f, -0.009f);
anim.enabled = false;
transform.Rotate(0.5f, 0, 0);

I want to do this same thing but in reverse and faster, and I’ve tried everything I can think of (coroutines with waiting, checking if the rotation is more than the default, but all of them have some problem associated with them. Hell, I even tried using an animation for it but since it doesn’t let me make an animation just for rotation that didn’t work either!

Further info:

  • I’m only calling the function once (when you let go of a button)
  • I tried doing a reverse counter like this:

for(int i = 0;i <= count;i++) { Vector3 translate = new Vector3(0, -0.01f, 0.009f); transform.Rotate(-0.5f, 0, 0); transform.Translate(translate); }
But that just makes it instantly go to the default position, and even if it did work it wouldn’t be faster, just the same but in reverse. I made it work like that last night, but I don’t have the code from that anymore, and even then that wouldn’t solve the issue of it not going fast enough. Is there a way to do this with animations that I’ve overlooked, or some other way to do it in this way? Thanks in advance!

A coroutine should work for this. However you could use Invoke. Something like this:

void Change()
     //Do the rotation and translation
     if( //some condition is met) Invoke ("Change",  0.2f);