简体   繁体   English

一起使用Lerp Position和Slerp Rotation(统一)

[英]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. 这就是我要尝试的操作,当我单击UI元素时,摄像头会平滑旋转(以查看目标),同时在目标上方移动。

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# 这是代码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 使用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 用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. 谢谢你的帮助。

In CoroutineMoveRotationCamera , you need to update the relativePostion and rotation inside the while loop. CoroutineMoveRotationCamera中 ,您需要更新while循环内的relativePostion旋转

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. 现在,相机会根据您在while循环开始之前拍摄的矢量快照进行旋转。

Add the following code after yield statement. 在yield语句之后添加以下代码。

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

在启动协程之前,应使用成员变量而不是函数本地变量保存初始变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM