一起使用Lerp Position和Slerp Rotation(统一) [英] Use Lerp Position and Slerp Rotation together (Unity)

查看:198
本文介绍了一起使用Lerp Position和Slerp Rotation(统一)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要尝试的操作,当我单击UI元素时,相机会平稳旋转(以查看目标),同时在目标上方移动.

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.

为此,我使用两个协程,一个用于Lerp位置,另一个用于Slerp旋转.

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 ;)

这是代码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));
        }
    }

使用Lerp移动相机的位置

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;
}

使用Slerp旋转相机

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;
}

感谢您的帮助.

推荐答案

CoroutineMoveRotationCamera 中,您需要更新内部的 relativePostion rotation while循环.

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

相机移动时,相对位置正在改变.现在,您的相机会根据您在while循环开始之前拍摄的矢量快照进行旋转.

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.

在yield语句之后添加以下代码.

Add the following code after yield statement.

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

这篇关于一起使用Lerp Position和Slerp Rotation(统一)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆