Posted September 10, 201510 yr I'm trying to figure out a way to force a player to look at a nearby entity. I know that this can be done the opposite way (forcing an entity to look at a player) using: <entity>.getLookHelper().setLookPosition(<player>.posX, <player>.posY + (double)<player>.getEyeHeight(), <player>.posZ, 10.0F, (float)<entity>.getVerticalFaceSpeed()); But seeing as a player entity doesn't have the .getLookHelper() method, I'm not sure how to do this a similar way. I understand that a player's position and rotation can be set with methods like .setPositionAndRotation(x, y, z, yaw, pitch), where yaw and pitch would determine rotation, but I'm not sure how to find/calculate what yaw and pitch to use to direct the player to look at the entity. https://i.gyazo.com/9d22a3d74363977ac76d662e7c22effb.png[/img]
September 10, 201510 yr It involves lots of trigonometry, but basically you subtract the target's position from the player's position on both the horizontal axes and run that through atan to get the angle, then subtract the player's current rotation yaw from that angle and make sure it's clamped between -180 and 180. I found I had to add 90 to it after the fact, probably as the result of some quirk in Minecraft's coordinate system. You can use similar math for the vertical axis, but be sure to include the entities' respective heights and/or eye positions in your calculations. Once you have both angles, call player#setAngles(yaw, pitch) and you're good to go. Perhaps consider setting the player's head yaw instead of their entire body, depending on your situation. I do this on the client side, btw, but you may want to do the calculations on both sides. http://i.imgur.com/NdrFdld.png[/img]
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.