Headshot detection?

Discussion in 'Plugin Development' started by DarkBladee12, Mar 1, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    DarkBladee12

    Hey guys, I'm trying to check if a player has shot another one an arrow in the head and I'm checking the location of the arrow in the EntityDamageByEntityEvent, but I'm always getting the same y-coord even when I hit the other player anywhere else... Do you know any solutions for this problem or is it not possible so far?
     
  2. Offline

    Tomskied

    Post some code, what you are doing should work.

    pseudo.
    Code:java
    1. double projectile_height = arrow.getLocation().getY();
    2. double player_bodyheight = 1.35;
    3. if(projectileheight>bodyheight){
    4. //Arrow to the face ensues
    5. }
     
  3. Offline

    DarkBladee12

    Tomskied that isn't working for me... I always get the same y-coord even if I don't shoot him into the head! That's my code so far:
    Code:
    @EventHandler(priority = EventPriority.NORMAL)
        public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
            if (!(event.getDamager() instanceof Arrow)) {
                return;
            }
            Arrow arw = (Arrow) event.getDamager();
            Entity shooter = arw.getShooter();
            if (!(shooter instanceof Player)) {
                return;
            }
            Player p = (Player) shooter;
            if (!plugin.weapon.containsKey(p.getName())) {
                return;
            }
            String weapon = plugin.weapon.get(p.getName());
            Gun g = new Gun(weapon, p, plugin);
            if (event.getEntity() instanceof Player) {
                Player damaged = (Player) event.getEntity();
                if (plugin.wu.isHeadshot(arw, damaged)) {
                    event.setDamage(g.getHeadshotDamage());
                    if (plugin.messagesEnabled && plugin.broadcastHeadshot) {
                        Bukkit.broadcastMessage(plugin.Headshot.replace("%shooter%", g.getHolderName()).replace("%player%", damaged.getName()));
                    }
                    return;
                }
            }
            event.setDamage(g.getDamage());
        }
     
        public boolean isHeadshot(Arrow a, Entity e) {
            double y = a.getLocation().getY();
            double y2 = ((LivingEntity) e).getEyeLocation().getY();
            double distance = Math.abs(y - y2);
            return distance <= 0.5D;
        }
     
  4. Offline

    Tomskied

    DarkBladee12 Try getting the projectile first.
    Code:java
    1.  
    2. if(event.getCause() != DamageCause.PROJECTILE){
    3. return;
    4. }
    5. Projectile proj = (Projectile) event.getDamager();
    6.  
    7. //check if arrow then do code from the proj object.
     
  5. Offline

    DarkBladee12

    Tomskied I'll try that, but why shouldn't it work for the arrow object?
     
  6. Offline

    Tomskied

    @DarkBladee12Perhaps because arrow extends Projectile, but I really dont know, thats just guessing
     
  7. Offline

    DarkBladee12

    Tomskied doesn't work for me too, the y-coord is always the same... :(
     
  8. Offline

    Tomskied

    DarkBladee12 Are you printing out proj.getLocation().getY() ?
     
  9. Offline

    DarkBladee12

    Tomskied I found out what the problem is... My arrow has too much velocity so the location tracking is bugged, I just decreased it and now it's working well! Thanks anyway ;)

    Tomskied hmm I'm increasing the velocity like "arw.setVelocity(arw.getVelocity().multiply(14));". I need much speed that the arrow hasn't much bullet drop, but how can I increase it so that the coords aren't buggy?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  10. Offline

    Tomskied

    DarkBladee12 Mmm, not sure. I guess you would have to find the limit to the speed. You could also try a Projectile hit event?
     
  11. Offline

    Technius

    Maybe you could do some ray tracing to see where the arrow hits.
     
  12. Offline

    DarkBladee12

    Technius The problem is, that the tracking is buggy when the projectile has too much speed, it even looks strange when flying...

    If I multiply it by 3 it's fine but any higher value will bug the tracking of the projectile!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
Thread Status:
Not open for further replies.

Share This Page