c# - Unity Viewbob 使用 lerp 随机回弹 1 帧
问题描述
我的 ViewBobScript 有问题,它会弹回 lerps 开始位置 1 帧。
这是我的问题的娱乐/视频:(由于视频的帧速率与游戏不匹配,它可能看起来好像 lerp 不流畅,而实际上是。问题是开始和结束时的快照视频,并且似乎以不规则的时间间隔出现)
这是我的代码:
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,我将不胜感激,因此我可以从中学习!
解决方案
我会采取不同的方法,添加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);
}
推荐阅读
- powershell - PowerShell findstr 字符串中的通配符
- swift - 如何使用 UISwitch 更改变量的值?
- django - 用相关的用户字段替换下拉查找
- npm - npm install 使用 node-gyp 从 CLI 工作,从 TeamCity 失败
- python - 如何运行其函数可以作为线程调用的对象
- python - 如何在linux中比较两个巨大的文本文件并获得差异
- r - R:如何将长数字转换为字符串以节省精度
- java - Apache POI - 在 Word 文件中编辑图表数据时,它返回到表单中定义的数据
- python - 根据另一列计算一列中的重复值
- python - 为什么我在使用 sep 函数时会出错?