首页 > 解决方案 > Unity Viewbob 使用 lerp 随机回弹 1 帧

问题描述

我的 ViewBobScript 有问题,它会弹回 lerps 开始位置 1 帧。

这是我的问题的娱乐/视频:(由于视频的帧速率与游戏不匹配,它可能看起来好像 lerp 不流畅,而实际上是。问题是开始和结束时的快照视频,并且似乎以不规则的时间间隔出现)

https://youtu.be/UOz2pQcMFjY

这是我的代码:

private float aTimer;
private int viewBobPath = 1;
private float startViewBob;
private float viewBobPathTimer;

private void ViewBob()
{
    startViewBob = viewBobSpeed / 2;

    aTimer += Time.deltaTime;
    viewBobPathTimer += Time.deltaTime; 

    if (aTimer > startViewBob * viewBobPath) { viewBobPath++; }
    if (viewBobPathTimer > startViewBob) { viewBobPathTimer = 0; } 

    if (aTimer < (viewBobSpeed * 2))
    {
        switch (viewBobPath)
        {
            case 1:
                playerCam.transform.localPosition = Vector3.Lerp(new Vector3(-viewBobHorizontal / 2, 0, 0), new Vector3(0, -viewBobVertical, 0), viewBobPathTimer);
                break;
            case 2:
                playerCam.transform.localPosition = Vector3.Lerp(new Vector3(0, -viewBobVertical, 0), new Vector3(viewBobHorizontal / 2, 0, 0), viewBobPathTimer);
                break;
            case 3:
                playerCam.transform.localPosition = Vector3.Lerp(new Vector3(viewBobHorizontal / 2, 0, 0), new Vector3(0, -viewBobVertical, 0), viewBobPathTimer);
                break;
            case 4:
                playerCam.transform.localPosition = Vector3.Lerp(new Vector3(0, -viewBobVertical, 0), new Vector3(-viewBobHorizontal / 2, 0, 0), viewBobPathTimer);
                break;
        }
    }
    else { aTimer = 0; viewBobPathTimer = 0; viewBobPath = 1; }

}

另外,我知道统一角色控制器有 ViewBobbing。但这不是我的应用程序的选项。

非常感谢你的帮助!

编辑:这是从更新函数调用的,虽然我已经尝试过 Fixed 和 Late update 都不会改变、解决或减少问题。

尽管我的脚本有效并且我正在继续我的项目,但如果您知道我上面的代码为什么不起作用/是否完成了 Snap,我将不胜感激,因此我可以从中学习!

标签: c#unity3dcameralerp

解决方案


我会采取不同的方法,添加Time.deltaTime到一个字段,然后计算该t字段对应的路径和参数。 Mathf.Repeat对此很有用。

[SerializeField] int pathIndex;
[SerializeField] float pathT;
[SerializeField] float aTimer;

[SerializeField] float pathDuration = 1f; // how long each path takes in seconds

private void ViewBob()
{
    // update aTimer but wrap around at pathDuration x 4 ( [0, 4pD) )
    aTimer = Mathf.Repeat(aTimer + Time.deltaTime, pathDuration * 4f);

    // How many path durations is aTimer along? ( {0, 1, 2, 3} )
    pathIndex = Mathf.FloorToInt(aTimer/pathDuration);

    // How far into the current path duration is aTimer?  ( [0,1) )
    pathT = Mathf.Repeat(aTimer, pathDuration) / pathDuration;

    Vector3 fromVector;
    Vector3 toVector;

    // assign to/from vectors based on pathIndex
    switch (pathIndex)
    {
        default:
        case 0:
            fromVector = new Vector3(-viewBobHorizontal / 2, 0, 0);
            toVector = new Vector3(0, -viewBobVertical, 0);
            break;
        case 1:
            fromVector = new Vector3(0, -viewBobVertical, 0);
            toVector = new Vector3(viewBobHorizontal / 2, 0, 0);
            break;
        case 2:
            fromVector = new Vector3(viewBobHorizontal / 2, 0, 0);
            toVector = new Vector3(0, -viewBobVertical, 0);
            break;
        case 3:
            fromVector = new Vector3(0, -viewBobVertical, 0);
            toVector = new Vector3(-viewBobHorizontal / 2, 0, 0);
            break;
    }

    // lerp position based on pathT and the to/from vectors
    playerCam.transform.localPosition = Vector3.Lerp(fromVector, toVector, pathT);
}

推荐阅读