我正在为我的世界服务器制作一个自定义的'枪'插件。我的问题是,当试图检测射弹落在哪里时,射弹和被击中的玩家之间的位置差异太大,无法通过我的爆头方法检测到。我对"isHeadshot"使用弹丸跟踪的唯一原因是由于调试试验。
这是我的拍摄方法
public void shootGun(Player p) {
if (getFirearmAction().isDelayed(p))
return;
double shiftYaw = (p.getLocation().getYaw() + 180) * (Math.PI/180);
Vector shiftVec = new Vector(Math.cos(shiftYaw), 0.0D, Math.sin(shiftYaw)); //TODO: add bullet_spread
for (int i = 0; i < projectileInfo.getAmount(); i++) {
Projectile proj = (Projectile)p.getWorld().spawn(p.getEyeLocation().toVector().add(shiftVec.multiply(0.2D)).toLocation(p.getWorld()), getProjectileInfo().getProjectileClass());
proj.setVelocity(UtilMethods.getBulletVelocity(p).multiply(projectileInfo.getSpeed()));
proj.setShooter(p);
}
projectileInfo.playShootGunSound(p);
getFirearmAction().performAction(p);
}
这是"命中"的听众
@EventHandler
public void onEntDamage(EntityDamageByEntityEvent event) {
if (event.getDamager() instanceof Projectile) {
Projectile p = (Projectile)event.getDamager();
if (p.getShooter() instanceof Player) {
Player player = (Player)p.getShooter();
Gun gun = UtilMethods.getGun(player);
if (gun != null) {
int damage = gun.getProjectileInfo().getDamage();
if (event.getEntity() instanceof Player) {
Player ent = (Player)event.getEntity();
if (UtilMethods.isHeadShot(ent, p))
damage *= 2;
}
event.setDamage(damage);
}
}
}
}
最后,这是[isHeadshot,isInCuboid和bulletVelocity]方法。前两种是试图找出失败的爆头检测的替代方法......getBulletVelocity 方法设置弹丸的正确速度和方向。(在本例中,我正在使用 Snowball.class(
public static boolean isHeadShot(Player victim, Projectile projectile) {
Location locA = new Location(victim.getWorld(), victim.getEyeLocation().getX() -0.5, victim.getEyeLocation().getY() - 0.5, victim.getEyeLocation().getZ() - 0.5);
Location locB = new Location(victim.getWorld(), victim.getEyeLocation().getX() +0.5, victim.getEyeLocation().getY() + 0.5, victim.getEyeLocation().getZ() + 0.5);
for (double i = 0; i < 256; i+=0.8D) {
System.out.println(projectile.getLocation() + " | " + victim.getLocation());
projectile.getLocation().add(projectile.getVelocity().normalize().multiply(i));
if (isInCuboid(locA, locB, projectile.getLocation())) {
System.out.println(i);
return true;
}
}
return false;
}
public static boolean isInCuboid(Location min, Location max, Location varying) {
double[] locs = new double[2];
locs[0] = min.getX();
locs[1] = max.getX();
Arrays.sort(locs);
if (varying.getX() > locs[1] || varying.getX() < locs[0])
return false;
locs[0] = min.getY();
locs[1] = max.getY();
Arrays.sort(locs);
if (varying.getY() > locs[1] || varying.getY() < locs[0])
return false;
locs[0] = min.getZ();
locs[1] = max.getZ();
Arrays.sort(locs);
if (varying.getZ() > locs[1] || varying.getZ() < locs[0])
return false;
return true;
}
public static Vector getBulletVelocity(Player shooter) {
double yaw = Math.toRadians((-shooter.getLocation().getYaw() - 90.0f));
double pitch = Math.toRadians(-shooter.getLocation().getPitch());
double x = Math.cos(pitch) * Math.cos(yaw);
double y = Math.sin(pitch);
double z = -Math.sin(yaw) * Math.cos(pitch);
Vector dirVec = new Vector(x, y, z).normalize();
return dirVec;
}
我想要一些帮助,因为当子弹造成伤害时,子弹似乎已经穿过了玩家的命中箱,无法再被检测到是爆头。有什么建议吗?
这是位置差异。第一个位置是弹丸,第二个位置是受害者。但是,要命中,子弹必须一次穿过玩家。
[08:53:22] [服务器线程/信息]: 位置{世界=工艺世界{名称=San_Andreas},x=2501.8249706725665,y=73.62000000476837,z=1681.0750064188326,音高=0.0,偏航=0.0} |位置{world=CraftWorld{name=San_Andreas},x=2508.7144181513295,y=72.0,z=1671.9424013003206,pitch=0.3,yaw=-8.100003}
监听 projectileHitEvent 并检查弹丸的 y 坐标是否在被击中玩家眼睛位置的 y 坐标的一定距离内(x 和 z 坐标与确定玩家的爆头无关(。