Get if player is looking up or down

Discussion in 'Spigot Plugin Development' started by Soolz, Jul 10, 2018.

  1. So with this code, we can get the direction of a player is looking, but we can't get if he looks up

    Code (Java):

        private String getDirection(Player player) {
            int degrees = (Math.round(player.getLocation().getYaw()) + 270) % 360;
            if (degrees <= 22) return "North";
            if (degrees <= 67) return "Northeast";
            if (degrees <= 112) return "East";
            if (degrees <= 157) return "Southeast";
            if (degrees <= 202) return "South";
            if (degrees <= 247) return "Southwest";
            if (degrees <= 292) return "West";
            if (degrees <= 337) return "Northwest";
            if (degrees <= 359) return "North";
            return null;
        }
     
  2. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    Very simple. If you press F3 in your Minecraft client to get the debug info, you can see your pitch and yaw. Here you're using yaw - that's 360 degrees on the horizontal axis, and you now need the vertical (so to speak, since this is 2D in a 3D environment). Check in your client what the center - looking straight forward - is. I'm fairly sure it's 0. So if it's more than 0 you're looking up, less than and you're looking down (possibly vice versa). Remember to double check this in the client's debug info.
     
    • Like Like x 1
  3. I don't quite understand what I have to do (probably because I don't speak English)

    But when I look down I see 90 and up -90
     
  4. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    So if it's more than 90 you're looking down, less than 90 and you're looking up. Kinda simple really.
     
  5. noooo. you had it right the first time. from the javadocs:
    • A pitch of 0 represents level forward facing.
    • A pitch of 90 represents downward facing, or negative y direction.
    • A pitch of -90 represents upward facing, or positive y direction.
     
    • Informative Informative x 1
  6. You need to compare pitch to get the player's up and down position, yaw only measures side to side.