You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

2D移动炮塔抛射物拦截移动目标异常问题排查

2D太空游戏前置瞄准偏差问题

我正在开发一款带炮塔的2D太空游戏,炮塔发射恒速抛射物拦截移动目标,当前存在以下异常情况:

  • 我方舰船与NPC舰船运动方向不同或非近似同向时,拦截效果正常
  • 同向运动时,炮塔发射的抛射物会轻微落在NPC舰船后方,但炮塔完成转向后能持续命中
  • 我方舰船静止时,可持续命中NPC舰船;但NPC舰船静止时,会向我舰船前方轻微射击

相关代码

主瞄准逻辑

Vector2 toTarget = (targetShip.GlobalPosition) - GlobalPosition;
Vector2 targetVel = (targetShip.dir * targetShip.speed);
Vector2 gunVelocity = (Player.currentShip.dir * Player.currentShip.playerSpeed);
Vector2 vr = targetVel - gunVelocity;

float t = AimAhead(toTarget, vr);

if(t > 0f)
{
    Vector2 aimPoint = targetShip.GlobalPosition + vr * t;
    dir = aimPoint - Player.currentShip.GlobalPosition; 
    GlobalRotation = Mathf.LerpAngle(GlobalRotation, dir.Angle(), turnRate * delta);
}
else
{
    Vector2 aimPoint = targetShip.GlobalPosition;
    dir = aimPoint - Player.currentShip.GlobalPosition;
    GlobalRotation = Mathf.LerpAngle(GlobalRotation, dir.Angle(), turnRate * delta);
}

前置瞄准计算函数

float a = vr.Dot(vr) - (projectile.speed * projectile.speed );
float b = 2 * vr.Dot(toTarget);
float c = toTarget.Dot(toTarget);

float det = b * b - 4 * a * c;

if (det > 0f)
{
    return 2 * c / (Mathf.Sqrt(det) - b);
}
else
{
    return -1f;
}

内容的提问来源于stack exchange,提问作者GoldSpark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 13:15:30