如何从高处计算弹丸的射程?
how to calculate the range of a projectile from a height?
玩家投射图
我想要做的是让玩家从已知速度、离地高度、角度和重力的点起飞。
我想知道玩家在再次到达地面之前要穿过的距离。
我试图用下面的公式计算它,但它似乎没有给我正确的结果:
public static float Range(float velocity, Vector3 direction, float angle, float gravity, float heightFromGround)
{
float xVelocity = velocity * direction.x;
float yVelocity = velocity * direction.y;
//R = Vx * [Vy + √(Vy² + 2 * g * h)] / g
float result = (xVelocity * (yVelocity + Mathf.Sqrt(Mathf.Pow(yVelocity, 2) + 2 * Mathf.Abs(gravity) * heightFromGround) / Mathf.Abs(gravity)));
return result;
}
更多详情:
我正在做的是我有一个生成平台和挂钩的无限关卡。
我想要的是在最后一个生成的钩子之后,平台将准确放置在玩家将到达的位置 position.y = 0
这就是我试图实现的方式:
private void HookSpawnerFinished(TrackPart lastHook)
{
hookSpawnerActive = false;
Vector3 lastHookGroundPosition = new Vector3(lastHook.transform.position.x, 0);
float hookRangeToGround = HookRangeToGround(lastHook);
Vector3 platformPosition = new Vector3(lastHook.transform.position.x + hookRangeToGround, 0);
ActivatePlatformSpawner(platformPosition);
}
private float HookRangeToGround(TrackPart lastHook)
{
print($"last hook Y position: {lastHook.transform.position.y}");
float DistanceToGround = PhysicsHelper.ProjectileMotion.Range(hookAbilityParameters.MaxHookForce, MathHelper.DegreeToVector2(hookAbilityParameters.MinExitAngle), hookAbilityParameters.MinExitAngle, hookAbilityParameters.Gravity, lastHook.transform.position.y);
return DistanceToGround;
}
DistanceToGround
值太大了,无论玩家能达到多少,平台都离得更远。
我已经使用了资产商店中的这个 trajectory predictor 资产,
它给了我正确的射弹运动弧度,但我还没有找到一种方法来弄清楚如何从资产代码计算范围。
但如您所见,我想要生成的平台远非如此。所以总而言之,玩家和轨迹预测器的数学都是 "matched" 但我必须计算范围的函数不是。
你的公式应该是重力除以 y 的平方根 + 速度。
试试这个
float result = xVelocity * (yVelocity + Mathf.Sqrt(Mathf.Pow(yVelocity, 2) + 2 * Mathf.Abs(gravity) * heightFromGround)) / Mathf.Abs(gravity);
你在做
//R = Vx * [Vy + √(Vy² + 2 * g * h) / g]
而不是
//R = Vx * [Vy + √(Vy² + 2 * g * h)] / g
玩家投射图
我想要做的是让玩家从已知速度、离地高度、角度和重力的点起飞。 我想知道玩家在再次到达地面之前要穿过的距离。
我试图用下面的公式计算它,但它似乎没有给我正确的结果:
public static float Range(float velocity, Vector3 direction, float angle, float gravity, float heightFromGround)
{
float xVelocity = velocity * direction.x;
float yVelocity = velocity * direction.y;
//R = Vx * [Vy + √(Vy² + 2 * g * h)] / g
float result = (xVelocity * (yVelocity + Mathf.Sqrt(Mathf.Pow(yVelocity, 2) + 2 * Mathf.Abs(gravity) * heightFromGround) / Mathf.Abs(gravity)));
return result;
}
更多详情:
我正在做的是我有一个生成平台和挂钩的无限关卡。
我想要的是在最后一个生成的钩子之后,平台将准确放置在玩家将到达的位置 position.y = 0
这就是我试图实现的方式:
private void HookSpawnerFinished(TrackPart lastHook)
{
hookSpawnerActive = false;
Vector3 lastHookGroundPosition = new Vector3(lastHook.transform.position.x, 0);
float hookRangeToGround = HookRangeToGround(lastHook);
Vector3 platformPosition = new Vector3(lastHook.transform.position.x + hookRangeToGround, 0);
ActivatePlatformSpawner(platformPosition);
}
private float HookRangeToGround(TrackPart lastHook)
{
print($"last hook Y position: {lastHook.transform.position.y}");
float DistanceToGround = PhysicsHelper.ProjectileMotion.Range(hookAbilityParameters.MaxHookForce, MathHelper.DegreeToVector2(hookAbilityParameters.MinExitAngle), hookAbilityParameters.MinExitAngle, hookAbilityParameters.Gravity, lastHook.transform.position.y);
return DistanceToGround;
}
DistanceToGround
值太大了,无论玩家能达到多少,平台都离得更远。
我已经使用了资产商店中的这个 trajectory predictor 资产, 它给了我正确的射弹运动弧度,但我还没有找到一种方法来弄清楚如何从资产代码计算范围。
但如您所见,我想要生成的平台远非如此。所以总而言之,玩家和轨迹预测器的数学都是 "matched" 但我必须计算范围的函数不是。
你的公式应该是重力除以 y 的平方根 + 速度。
试试这个
float result = xVelocity * (yVelocity + Mathf.Sqrt(Mathf.Pow(yVelocity, 2) + 2 * Mathf.Abs(gravity) * heightFromGround)) / Mathf.Abs(gravity);
你在做
//R = Vx * [Vy + √(Vy² + 2 * g * h) / g]
而不是
//R = Vx * [Vy + √(Vy² + 2 * g * h)] / g