title: Use Lerp Position and Slerp Rotation together (Unity)
body: <p>This is what I try to do, When I click on a UI Element, the camera smoothly rotate (to look at the target) and simultaneously move on top of the target.</p>
  
  <p>To perform that I use a two Coroutine one for the Lerp Position and the other one for the Slerp Rotation.</p>
  
  <p>The issue is that the rotation doesn't work correctly, normally the camera should look down to see the top of the target but instead of doing this it look like the Camera first look at the target and after that move to his position.</p>
  
  <p>I hope this is understandable ;)</p>
  
  <p>Here is the code c#</p>
  
  <pre><code>Vector3 LocationProjectForPos = new Vector3(Loc_X, 100, Loc_Z);
  Vector3 LocationProjectForRot = new Vector3(Loc_X, Loc_Y, Loc_Z);
  Vector3 MainCameraPos = MainCamera.transform.position;
  
  if(!IsCameraMoving &amp; LocationProjectForPos != MainCameraPos)
          {
              StartCoroutine (CoroutineMovePositionCamera(LocationProjectForPos));
              StartCoroutine (CoroutineMoveRotationCamera(LocationProjectForRot));
          }
      }
  </code></pre>
  
  <p>Moving the position of the Camera with Lerp</p>
  
  <pre><code>public IEnumerator CoroutineMovePositionCamera(Vector3 LocationProject)
  {
      float lerpTime = 5f;
      float currentLerpTime = 0f;
  
      IsCameraMoving = true;
  
      Vector3 startPos =  MainCamera.transform.localPosition;
      Vector3 endPos =  LocationProject;
  
      while (lerpTime &gt; 0)
      {
          lerpTime -= Time.deltaTime;
          currentLerpTime += Time.deltaTime;
  
          if (currentLerpTime &gt; lerpTime) 
          {
              currentLerpTime = lerpTime;
          }
  
      float t = currentLerpTime / lerpTime;
      t = t*t*t * (t * (6f*t - 15f) + 10f);
      //t = t*t * (3f - 2f*t);
      //t = 1f - Mathf.Cos(t * Mathf.PI * 0.5f);
      MainCamera.transform.localPosition = Vector3.Lerp(startPos, endPos, t);
  
      yield return null;  
      }
      IsCameraMoving = false;
  }
  </code></pre>
  
  <p>Rotate the Camera with Slerp</p>
  
  <pre><code>public IEnumerator CoroutineMoveRotationCamera(Vector3 LocationProject)
  {
      float lerpTime = 5f;
      float currentLerpTime = 0f;
  
      IsCameraMoving = true;
  
      Vector3 relativePos =  LocationProject - MainCamera.transform.localPosition;
      Quaternion rotation = Quaternion.LookRotation(relativePos);
      Quaternion current = MainCamera.transform.localRotation;
  
      while (lerpTime &gt; 0)
      {
          lerpTime -= Time.deltaTime;
          currentLerpTime += Time.deltaTime;
  
          if (currentLerpTime &gt; lerpTime) 
          {
              currentLerpTime = lerpTime;
          }
  
      float t = currentLerpTime / lerpTime;
      t = t*t*t * (t * (6f*t - 15f) + 10f);
      //t = t*t * (3f - 2f*t);
      //t = 1f - Mathf.Cos(t * Mathf.PI * 0.5f);
      MainCamera.transform.localRotation = Quaternion.Slerp(current, rotation, t);
  
      yield return null;  
      }
      IsCameraMoving = false;
  }
  </code></pre>
  
  <p>Thanks for your help.</p>