Jump to content

[SOLVED][1.8] Make Player Look at Entity


Recommended Posts

Posted

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.

Posted

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.

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.