Search code examples
c#unity-game-enginecoroutinelerp

Use Lerp Position and Slerp Rotation together (Unity)


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.

To perform that I use a two Coroutine one for the Lerp Position and the other one for the Slerp Rotation.

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.

I hope this is understandable ;)

Here is the code c#

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 & LocationProjectForPos != MainCameraPos)
        {
            StartCoroutine (CoroutineMovePositionCamera(LocationProjectForPos));
            StartCoroutine (CoroutineMoveRotationCamera(LocationProjectForRot));
        }
    }

Moving the position of the Camera with Lerp

public IEnumerator CoroutineMovePositionCamera(Vector3 LocationProject)
{
    float lerpTime = 5f;
    float currentLerpTime = 0f;

    IsCameraMoving = true;

    Vector3 startPos =  MainCamera.transform.localPosition;
    Vector3 endPos =  LocationProject;

    while (lerpTime > 0)
    {
        lerpTime -= Time.deltaTime;
        currentLerpTime += Time.deltaTime;

        if (currentLerpTime > 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;
}

Rotate the Camera with Slerp

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 > 0)
    {
        lerpTime -= Time.deltaTime;
        currentLerpTime += Time.deltaTime;

        if (currentLerpTime > 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;
}

Thanks for your help.


Solution

  • In CoroutineMoveRotationCamera, you need to update the relativePostion and rotation inside the while loop.

    The relative position is changing while the camera is moving. Right now, your camera rotates based on the vector snapshot you took before the while loop started.

    Add the following code after yield statement.

    relativePos = LocationProject - Camera.main.transform.position;
    rotation = Quaternion.LookRotation(relativePos);