使用 lerp 的 Unity Viewbob 随机返回 1 帧
Unity Viewbob using lerp snaps back for 1 frame at random
我的 ViewBobScript 有问题,它会快速回到 lerps 开始位置 1 帧。
这是我的问题 Recreation/Video:
(由于视频的帧率与游戏不匹配,所以 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; }
}
另外,我知道unity character controller有ViewBobbing。但这不是我的应用程序的选项。
非常感谢您的帮助!
编辑:这是从更新功能调用的,虽然我已经尝试过修复和后期更新都没有改变,解决或减少问题。
尽管我的脚本可以运行并且我正在继续我的项目,但如果您知道为什么我上面的代码没有 work/did 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);
}
我的 ViewBobScript 有问题,它会快速回到 lerps 开始位置 1 帧。
这是我的问题 Recreation/Video: (由于视频的帧率与游戏不匹配,所以 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; }
}
另外,我知道unity character controller有ViewBobbing。但这不是我的应用程序的选项。
非常感谢您的帮助!
编辑:这是从更新功能调用的,虽然我已经尝试过修复和后期更新都没有改变,解决或减少问题。
尽管我的脚本可以运行并且我正在继续我的项目,但如果您知道为什么我上面的代码没有 work/did 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);
}