title: How do I add easing to a scripted transform animation in Unity?
body: <p>I've got a setup right now where I have a 3D object with an empty object parented to it inside the object. When you press one button, an empty object that the camera is following rotates 90 degrees. If you press the other button, it rotates the other direction in 90 degrees. The result is that the camera can spin around the object 4 times before it makes a complete rotation.</p>
  <p>Currently it works well, but I'm trying to figure out how I add some easing to the animation so it doesn't look so rough. I know a little about working with curves in animation but I'm not sure how I can apply that to code (or if it's even the best way to do things)</p>
  <pre><code>    public InputMaster controls;
      private bool isSpinning;
      private float rotationSpeed = 0.3f;
  
      IEnumerator RotateMe(Vector3 byangles, float intime)
      {
          var fromangle = transform.rotation;
          var toangle = Quaternion.Euler(transform.eulerAngles + byangles);
          for (var t = 0f; t &lt; 1; t += Time.deltaTime / intime)
          {
              transform.rotation = Quaternion.Slerp(fromangle, toangle, t);
              yield return null;
              transform.rotation = toangle;
          }
  
              Debug.Log(&quot;finished rotation&quot;);
              isSpinning = false;
              Debug.Log(&quot;isSpinning now false&quot;);
      }
  
  
  </code></pre>
  <p>code above is where I create a coroutine that says how it is going to transform. One thing that confuses me here a little is that if I don't have the line that says <code>transform.rotation = toangle;</code> , the rotation comes out at like 89.5 degrees or something, and if you do it multiple times it goes several degrees off. Not sure why that happens.</p>
  <pre><code>    void Rotation(float amount)
      {
          Debug.Log(&quot;rotation number is &quot; + amount);
  
          float holdingRotate = controls.Player.Camera.ReadValue&lt;float&gt;();
  
          if (holdingRotate == 1 &amp;&amp; isSpinning == false)
          {
              isSpinning = true;
              Debug.Log(&quot;isSpinning now true&quot;);
              StartCoroutine(RotateMe(Vector3.up * 90, rotationSpeed));
  
          }
          else if (holdingRotate == -1 &amp;&amp; isSpinning == false)
          {
              isSpinning = true;
              Debug.Log(&quot;isSpinning now true&quot;);
              StartCoroutine(RotateMe(Vector3.up * -90, rotationSpeed));
          }
      }
  </code></pre>
  <p>and this part is where the animation gets called up. Any help much appreciated.</p>