线性移动物体统一的未来位置

Future Position for linearly moving object unity

一个物体以Starting Speed(SP)为8,加速度为0.02f/秒,直线向前运动。 SP is curSpeed= minSpeed (1) minSpeed= 8 (2) maxSpeed=20 (3) curSpeed += acceleration * Time.deltaTime - 直到达到 maxSpeed 10、15 或 30 秒后物体会在哪里? 我需要在几秒钟后从当前玩家位置提前生成一个对象到其未来位置以同步游戏玩法。假设玩家处于零位置,十秒后距离将变为 90,我将提前在 90 处生成对象供玩家捕捉。有谁知道如何解决这个问题?

可以用数学来解决,但我有另一个想法。将一个空的游戏对象作为移动对象的子对象。将其沿运动方向移动所需的距离。使用

在脚本中引用此游戏对象的转换
public Transform spawnPoint

并在检查器中赋值。现在您可以提前 spawnPoint.position 生成您的对象,无论速度或时间如何,它与您的玩家的距离相同。

数学解法: 首先,您需要将起始位置存储到 Vector3 中。我们称它为 startPos。然后我们需要一个运动方向向量。例如(但要看你的动作)

Vector3 direction = Vector3.forward;

那么你说的是两种类型的运动:加速运动和线性运动。

让我们计算加速时间:

float aTime = (maxSpeed - minSpeed)/acceleration;

我们来计算加速结束的位置:

Vector3 aEndPos = startPos + direction * (minSpeed * aTime + acceleration * Mathf.Pow(aTime, 2) / 2f);

现在我们可以写出计算距离的方法了:

Vector3 GetDistance(float time)
{
  Vector3 direction = Vector3.forward;
  float aTime = (maxSpeed - minSpeed)/acceleration;
  Vector3 aEndPos = startPos + direction * (minSpeed * aTime + acceleration * Mathf.Pow(aTime, 2) / 2f);

  if (time <= aTime) //if we are in accelerating movement
  {
      return startPos + direction * (minSpeed * time + acceleration * Mathf.Pow(time, 2) / 2f);
  } 
  else // we are in linear
  {
      return aEndPos + direction * maxSpeed * (time - aTime)
  }
}