Projectile headshot

Discussion in 'Plugin Development' started by MCCoding, Jan 12, 2014.

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

    MCCoding

    I'm trying to make a headshot plugin but the math to get the location of the players head is not working and i can't get it right. Here is the math i'm using now what is happening is if i shoot the player from under it and hit it in the head it wont be a head shot, if i'm above the player and shoot it in the feet it will be a headshot and if i'm on the same level as the target and shoot at it's feet it's always a headshot.

    Code:
        double y = proj.getLocation().getY(); //Location of the projectile on enitytDamage
            double hitY = damaged.getLocation().getY(); //location of the damaged target
            boolean headshot = y - hitY > 1.35D; //math to check if it's a head shot
     
  2. Offline

    newboyhun

    MCCoding
    This is what i use:
    EntityDamageByEntityEvent
    Code:
    event.getDamager().getLocation().distance(((Player) event.getEntity()).getEyeLocation()) < 0.4
     
  3. Offline

    MCCoding

    newboyhun
    Thanks but thats still not working, its now not detecting the headshot at all
     
  4. Offline

    newboyhun

    MCCoding
    Code:
           
        @EventHandler
        public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
            if (event.getDamager() instanceof Arrow && event.getEntity() instanceof Player) {
                if (event.getDamager().getLocation().distance(((Player) event.getEntity()).getEyeLocation()) < 0.4) {
                    event.setDamage(event.getDamage() * 2);
                }
            }
        }
     
  5. Offline

    MCCoding

    newboyhun
    I put that event in and it's not working, yes i have registers the events i added a broadcast message if someone got a headshot an the message was not broadcasted.
     
  6. Offline

    Maurdekye

    Try increasing the 0.4, maybe?
    Or possibly adding a System.out.println() to tell the distance of the arrow from the head, and debug it yourself.
     
  7. Offline

    newboyhun

    MCCoding
    Who are you trying on?
    It only works with players.
     
  8. Offline

    MCCoding

  9. Offline

    Deleted user

    MCCoding
    Here's something that I'm using that works somewhat consistantly.

    if (Math.floor(s.getLocation().getY()) == p.getPlayer().getLocation().getY()+1) {
    p.setHeadshoted(true);
    p.setLastDamager(((Player)s.getShooter()).getName());
    i+=0.5;
    } else {
    p.setHeadshoted(false);
    }

    Where s is a snowball, and p.getPlayer() returns the player that we want
     
  10. Offline

    MCCoding

    zombiekiller753
    That's still not working if i'm on the same level as it i will always get a head shot if i'm above or below i don't get a headshot, here is what i have

    Code:java
    1. if (Math.floor(proj.getLocation().getY()) == shotPlayer.getLocation().getY() +1) {
     
  11. Offline

    Deleted user

  12. Offline

    Garris0n

    I'd get the distance of the top of the head/bottom of the head from the eye location and then check the y value of the projectile compared to the head. More accurate and less intensive.
     
  13. Offline

    Deleted user

    Garris0n
    MCCoding

    Now that I think about it, you could customize the accuracy by doing something like

    double projectile_location = proj.getLocation().getY();
    double player_location = player.getEyeLocation().getY();
    if (player_location-0.5<= projectile_location && player_location+0.5 >= projectile_location) {
    //Do headshot stuff
    }

    This would customize it to have a search radius of 1 block around the player's eye. Changing the 0.5 to a smaller number shrinks the search area
     
    Garris0n likes this.
Thread Status:
Not open for further replies.

Share This Page